aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/users
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-04-14 22:07:46 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-04-14 22:07:46 +0200
commit1553e15d82b8a1ec4967a90d43b33274f8215c44 (patch)
treeee53fdbb32895153a1fd2470e1c51cf1d9a38e77 /client/angular/users
parent0c1cbbfe29d91c95f9c574b57adf067654b8b5b4 (diff)
downloadPeerTube-1553e15d82b8a1ec4967a90d43b33274f8215c44.tar.gz
PeerTube-1553e15d82b8a1ec4967a90d43b33274f8215c44.tar.zst
PeerTube-1553e15d82b8a1ec4967a90d43b33274f8215c44.zip
Implement user requests autorizations in the client side
Diffstat (limited to 'client/angular/users')
-rw-r--r--client/angular/users/components/login/login.component.ts17
-rw-r--r--client/angular/users/models/token.ts24
-rw-r--r--client/angular/users/models/user.ts20
-rw-r--r--client/angular/users/services/auth.service.ts49
4 files changed, 93 insertions, 17 deletions
diff --git a/client/angular/users/components/login/login.component.ts b/client/angular/users/components/login/login.component.ts
index 0881a3a15..35dea4f9b 100644
--- a/client/angular/users/components/login/login.component.ts
+++ b/client/angular/users/components/login/login.component.ts
@@ -3,7 +3,7 @@ import { Router } from 'angular2/router';
3 3
4import { AuthService } from '../../services/auth.service'; 4import { AuthService } from '../../services/auth.service';
5import { AuthStatus } from '../../models/authStatus'; 5import { AuthStatus } from '../../models/authStatus';
6import { Token } from '../../models/token'; 6import { User } from '../../models/user';
7 7
8@Component({ 8@Component({
9 selector: 'my-user-login', 9 selector: 'my-user-login',
@@ -17,16 +17,21 @@ export class UserLoginComponent {
17 login(username: string, password: string) { 17 login(username: string, password: string) {
18 this._authService.login(username, password).subscribe( 18 this._authService.login(username, password).subscribe(
19 result => { 19 result => {
20 if (result.error) return alert(result.error_description); 20 const user = new User(username, result);
21 21 user.save();
22 let token = new Token(result);
23 token.save();
24 22
25 this._authService.setStatus(AuthStatus.LoggedIn); 23 this._authService.setStatus(AuthStatus.LoggedIn);
26 24
27 this._router.navigate(['VideosList']); 25 this._router.navigate(['VideosList']);
28 }, 26 },
29 error => alert(error) 27 error => {
28 if (error.error === 'invalid_grant') {
29 alert('Credentials are invalid.');
30 }
31 else {
32 alert(`${error.error}: ${error.error_description}`)
33 }
34 }
30 ); 35 );
31 } 36 }
32} 37}
diff --git a/client/angular/users/models/token.ts b/client/angular/users/models/token.ts
index 688dfdc80..906bf501b 100644
--- a/client/angular/users/models/token.ts
+++ b/client/angular/users/models/token.ts
@@ -3,13 +3,27 @@ export class Token {
3 refresh_token: string; 3 refresh_token: string;
4 token_type: string; 4 token_type: string;
5 5
6 constructor (hash) { 6 constructor (hash?: any) {
7 this.access_token = hash.access_token; 7 if (hash) {
8 this.refresh_token = hash.refresh_token; 8 this.access_token = hash.access_token;
9 this.token_type = hash.token_type; 9 this.refresh_token = hash.refresh_token;
10 if (hash.token_type === 'bearer') {
11 this.token_type = 'Bearer';
12 } else {
13 this.token_type = hash.token_type;
14 }
15 }
10 } 16 }
11 17
12 save() { 18 static load(): Token {
19 return new Token({
20 access_token: localStorage.getItem('access_token'),
21 refresh_token: localStorage.getItem('refresh_token'),
22 token_type: localStorage.getItem('token_type')
23 });
24 }
25
26 save():void {
13 localStorage.setItem('access_token', this.access_token); 27 localStorage.setItem('access_token', this.access_token);
14 localStorage.setItem('refresh_token', this.refresh_token); 28 localStorage.setItem('refresh_token', this.refresh_token);
15 localStorage.setItem('token_type', this.token_type); 29 localStorage.setItem('token_type', this.token_type);
diff --git a/client/angular/users/models/user.ts b/client/angular/users/models/user.ts
new file mode 100644
index 000000000..2c56a6132
--- /dev/null
+++ b/client/angular/users/models/user.ts
@@ -0,0 +1,20 @@
1import { Token } from './token';
2
3export class User {
4 username: string;
5 token: Token;
6
7 constructor (username: string, hash_token: any) {
8 this.username = username;
9 this.token = new Token(hash_token);
10 }
11
12 static load(): User {
13 return new User(localStorage.getItem('username'), Token.load());
14 }
15
16 save(): void {
17 localStorage.setItem('username', this.username);
18 this.token.save();
19 }
20}
diff --git a/client/angular/users/services/auth.service.ts b/client/angular/users/services/auth.service.ts
index 80886346c..89412c3df 100644
--- a/client/angular/users/services/auth.service.ts
+++ b/client/angular/users/services/auth.service.ts
@@ -1,20 +1,23 @@
1import { Injectable } from 'angular2/core'; 1import { Injectable } from 'angular2/core';
2import { Http, Response, Headers, URLSearchParams } from 'angular2/http'; 2import { Http, Response, Headers, URLSearchParams, RequestOptions } from 'angular2/http';
3import { Observable, Subject } from 'rxjs/Rx'; 3import { Observable, Subject } from 'rxjs/Rx';
4 4
5import { AuthStatus } from '../models/authStatus'; 5import { AuthStatus } from '../models/authStatus';
6import { User } from '../models/user';
6 7
7@Injectable() 8@Injectable()
8export class AuthService { 9export class AuthService {
9 loginChanged$ = this._loginChanged.asObservable(); 10 loginChanged$;
10
11 private _loginChanged = new Subject<AuthStatus>();
12 11
12 private _loginChanged;
13 private _baseLoginUrl = '/api/v1/users/token'; 13 private _baseLoginUrl = '/api/v1/users/token';
14 private _clientId = '56f055587305d40b21904240'; 14 private _clientId = '56f055587305d40b21904240';
15 private _clientSecret = 'megustalabanana'; 15 private _clientSecret = 'megustalabanana';
16 16
17 constructor (private http: Http) {} 17 constructor (private http: Http) {
18 this._loginChanged = new Subject<AuthStatus>();
19 this.loginChanged$ = this._loginChanged.asObservable();
20 }
18 21
19 login(username: string, password: string) { 22 login(username: string, password: string) {
20 let body = new URLSearchParams(); 23 let body = new URLSearchParams();
@@ -42,12 +45,46 @@ export class AuthService {
42 // TODO make HTTP request 45 // TODO make HTTP request
43 } 46 }
44 47
48 getRequestHeader(): Headers {
49 return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` });
50 }
51
52 getAuthRequestOptions(): RequestOptions {
53 return new RequestOptions({ headers: this.getRequestHeader() });
54 }
55
56 getToken(): string {
57 return localStorage.getItem('access_token');
58 }
59
60 getTokenType(): string {
61 return localStorage.getItem('token_type');
62 }
63
64 getUser(): User {
65 if (this.isLoggedIn() === false) {
66 return null;
67 }
68
69 const user = User.load();
70
71 return user;
72 }
73
74 isLoggedIn(): boolean {
75 if (this.getToken()) {
76 return true;
77 } else {
78 return false;
79 }
80 }
81
45 setStatus(status: AuthStatus) { 82 setStatus(status: AuthStatus) {
46 this._loginChanged.next(status); 83 this._loginChanged.next(status);
47 } 84 }
48 85
49 private handleError (error: Response) { 86 private handleError (error: Response) {
50 console.error(error); 87 console.error(error);
51 return Observable.throw(error.json().error || 'Server error'); 88 return Observable.throw(error.json() || { error: 'Server error' });
52 } 89 }
53} 90}