diff options
Diffstat (limited to 'client/app/users')
-rw-r--r-- | client/app/users/login/login.component.ts | 8 | ||||
-rw-r--r-- | client/app/users/shared/auth.service.ts | 41 | ||||
-rw-r--r-- | client/app/users/shared/token.model.ts | 7 | ||||
-rw-r--r-- | client/app/users/shared/user.model.ts | 6 |
4 files changed, 32 insertions, 30 deletions
diff --git a/client/app/users/login/login.component.ts b/client/app/users/login/login.component.ts index 33590ad4c..8e369541d 100644 --- a/client/app/users/login/login.component.ts +++ b/client/app/users/login/login.component.ts | |||
@@ -10,17 +10,17 @@ import { AuthService, AuthStatus, User } from '../shared/index'; | |||
10 | }) | 10 | }) |
11 | 11 | ||
12 | export class UserLoginComponent { | 12 | export class UserLoginComponent { |
13 | constructor(private _authService: AuthService, private _router: Router) {} | 13 | constructor(private authService: AuthService, private router: Router) {} |
14 | 14 | ||
15 | login(username: string, password: string) { | 15 | login(username: string, password: string) { |
16 | this._authService.login(username, password).subscribe( | 16 | this.authService.login(username, password).subscribe( |
17 | result => { | 17 | result => { |
18 | const user = new User(username, result); | 18 | const user = new User(username, result); |
19 | user.save(); | 19 | user.save(); |
20 | 20 | ||
21 | this._authService.setStatus(AuthStatus.LoggedIn); | 21 | this.authService.setStatus(AuthStatus.LoggedIn); |
22 | 22 | ||
23 | this._router.navigate(['VideosList']); | 23 | this.router.navigate(['VideosList']); |
24 | }, | 24 | }, |
25 | error => { | 25 | error => { |
26 | if (error.error === 'invalid_grant') { | 26 | if (error.error === 'invalid_grant') { |
diff --git a/client/app/users/shared/auth.service.ts b/client/app/users/shared/auth.service.ts index 1cb042db5..b1da94436 100644 --- a/client/app/users/shared/auth.service.ts +++ b/client/app/users/shared/auth.service.ts | |||
@@ -7,27 +7,28 @@ import { User } from './user.model'; | |||
7 | 7 | ||
8 | @Injectable() | 8 | @Injectable() |
9 | export class AuthService { | 9 | export class AuthService { |
10 | loginChanged$; | 10 | private static BASE_LOGIN_URL = '/api/v1/users/token'; |
11 | private static BASE_CLIENT_URL = '/api/v1/users/client'; | ||
11 | 12 | ||
12 | private _loginChanged; | 13 | loginChangedSource: Observable<AuthStatus>; |
13 | private _baseLoginUrl = '/api/v1/users/token'; | ||
14 | private _baseClientUrl = '/api/v1/users/client'; | ||
15 | private _clientId = ''; | ||
16 | private _clientSecret = ''; | ||
17 | 14 | ||
18 | constructor (private http: Http) { | 15 | private loginChanged: Subject<AuthStatus>; |
19 | this._loginChanged = new Subject<AuthStatus>(); | 16 | private clientId: string; |
20 | this.loginChanged$ = this._loginChanged.asObservable(); | 17 | private clientSecret: string; |
18 | |||
19 | constructor(private http: Http) { | ||
20 | this.loginChanged = new Subject<AuthStatus>(); | ||
21 | this.loginChangedSource = this.loginChanged.asObservable(); | ||
21 | 22 | ||
22 | // Fetch the client_id/client_secret | 23 | // Fetch the client_id/client_secret |
23 | // FIXME: save in local storage? | 24 | // FIXME: save in local storage? |
24 | this.http.get(this._baseClientUrl) | 25 | this.http.get(AuthService.BASE_CLIENT_URL) |
25 | .map(res => res.json()) | 26 | .map(res => res.json()) |
26 | .catch(this.handleError) | 27 | .catch(this.handleError) |
27 | .subscribe( | 28 | .subscribe( |
28 | result => { | 29 | result => { |
29 | this._clientId = result.client_id; | 30 | this.clientId = result.client_id; |
30 | this._clientSecret = result.client_secret; | 31 | this.clientSecret = result.client_secret; |
31 | console.log('Client credentials loaded.'); | 32 | console.log('Client credentials loaded.'); |
32 | }, | 33 | }, |
33 | error => { | 34 | error => { |
@@ -38,8 +39,8 @@ export class AuthService { | |||
38 | 39 | ||
39 | login(username: string, password: string) { | 40 | login(username: string, password: string) { |
40 | let body = new URLSearchParams(); | 41 | let body = new URLSearchParams(); |
41 | body.set('client_id', this._clientId); | 42 | body.set('client_id', this.clientId); |
42 | body.set('client_secret', this._clientSecret); | 43 | body.set('client_secret', this.clientSecret); |
43 | body.set('response_type', 'code'); | 44 | body.set('response_type', 'code'); |
44 | body.set('grant_type', 'password'); | 45 | body.set('grant_type', 'password'); |
45 | body.set('scope', 'upload'); | 46 | body.set('scope', 'upload'); |
@@ -53,7 +54,7 @@ export class AuthService { | |||
53 | headers: headers | 54 | headers: headers |
54 | }; | 55 | }; |
55 | 56 | ||
56 | return this.http.post(this._baseLoginUrl, body.toString(), options) | 57 | return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) |
57 | .map(res => res.json()) | 58 | .map(res => res.json()) |
58 | .catch(this.handleError); | 59 | .catch(this.handleError); |
59 | } | 60 | } |
@@ -62,7 +63,7 @@ export class AuthService { | |||
62 | // TODO make HTTP request | 63 | // TODO make HTTP request |
63 | } | 64 | } |
64 | 65 | ||
65 | getRequestHeader(): Headers { | 66 | getRequestHeader() { |
66 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | 67 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); |
67 | } | 68 | } |
68 | 69 | ||
@@ -70,11 +71,11 @@ export class AuthService { | |||
70 | return new RequestOptions({ headers: this.getRequestHeader() }); | 71 | return new RequestOptions({ headers: this.getRequestHeader() }); |
71 | } | 72 | } |
72 | 73 | ||
73 | getToken(): string { | 74 | getToken() { |
74 | return localStorage.getItem('access_token'); | 75 | return localStorage.getItem('access_token'); |
75 | } | 76 | } |
76 | 77 | ||
77 | getTokenType(): string { | 78 | getTokenType() { |
78 | return localStorage.getItem('token_type'); | 79 | return localStorage.getItem('token_type'); |
79 | } | 80 | } |
80 | 81 | ||
@@ -88,7 +89,7 @@ export class AuthService { | |||
88 | return user; | 89 | return user; |
89 | } | 90 | } |
90 | 91 | ||
91 | isLoggedIn(): boolean { | 92 | isLoggedIn() { |
92 | if (this.getToken()) { | 93 | if (this.getToken()) { |
93 | return true; | 94 | return true; |
94 | } else { | 95 | } else { |
@@ -97,7 +98,7 @@ export class AuthService { | |||
97 | } | 98 | } |
98 | 99 | ||
99 | setStatus(status: AuthStatus) { | 100 | setStatus(status: AuthStatus) { |
100 | this._loginChanged.next(status); | 101 | this.loginChanged.next(status); |
101 | } | 102 | } |
102 | 103 | ||
103 | private handleError (error: Response) { | 104 | private handleError (error: Response) { |
diff --git a/client/app/users/shared/token.model.ts b/client/app/users/shared/token.model.ts index b7872e74a..021c83fad 100644 --- a/client/app/users/shared/token.model.ts +++ b/client/app/users/shared/token.model.ts | |||
@@ -3,7 +3,7 @@ export class Token { | |||
3 | refresh_token: string; | 3 | refresh_token: string; |
4 | token_type: string; | 4 | token_type: string; |
5 | 5 | ||
6 | static load(): Token { | 6 | static load() { |
7 | return new Token({ | 7 | return new Token({ |
8 | access_token: localStorage.getItem('access_token'), | 8 | access_token: localStorage.getItem('access_token'), |
9 | refresh_token: localStorage.getItem('refresh_token'), | 9 | refresh_token: localStorage.getItem('refresh_token'), |
@@ -11,10 +11,11 @@ export class Token { | |||
11 | }); | 11 | }); |
12 | } | 12 | } |
13 | 13 | ||
14 | constructor (hash?: any) { | 14 | constructor(hash?: any) { |
15 | if (hash) { | 15 | if (hash) { |
16 | this.access_token = hash.access_token; | 16 | this.access_token = hash.access_token; |
17 | this.refresh_token = hash.refresh_token; | 17 | this.refresh_token = hash.refresh_token; |
18 | |||
18 | if (hash.token_type === 'bearer') { | 19 | if (hash.token_type === 'bearer') { |
19 | this.token_type = 'Bearer'; | 20 | this.token_type = 'Bearer'; |
20 | } else { | 21 | } else { |
@@ -23,7 +24,7 @@ export class Token { | |||
23 | } | 24 | } |
24 | } | 25 | } |
25 | 26 | ||
26 | save():void { | 27 | save() { |
27 | localStorage.setItem('access_token', this.access_token); | 28 | localStorage.setItem('access_token', this.access_token); |
28 | localStorage.setItem('refresh_token', this.refresh_token); | 29 | localStorage.setItem('refresh_token', this.refresh_token); |
29 | localStorage.setItem('token_type', this.token_type); | 30 | localStorage.setItem('token_type', this.token_type); |
diff --git a/client/app/users/shared/user.model.ts b/client/app/users/shared/user.model.ts index 73fd4ddc0..ca0a5f26c 100644 --- a/client/app/users/shared/user.model.ts +++ b/client/app/users/shared/user.model.ts | |||
@@ -4,16 +4,16 @@ export class User { | |||
4 | username: string; | 4 | username: string; |
5 | token: Token; | 5 | token: Token; |
6 | 6 | ||
7 | static load(): User { | 7 | static load() { |
8 | return new User(localStorage.getItem('username'), Token.load()); | 8 | return new User(localStorage.getItem('username'), Token.load()); |
9 | } | 9 | } |
10 | 10 | ||
11 | constructor (username: string, hash_token: any) { | 11 | constructor(username: string, hash_token: any) { |
12 | this.username = username; | 12 | this.username = username; |
13 | this.token = new Token(hash_token); | 13 | this.token = new Token(hash_token); |
14 | } | 14 | } |
15 | 15 | ||
16 | save(): void { | 16 | save() { |
17 | localStorage.setItem('username', this.username); | 17 | localStorage.setItem('username', this.username); |
18 | this.token.save(); | 18 | this.token.save(); |
19 | } | 19 | } |