diff options
Diffstat (limited to 'client/app/users/shared/auth.service.ts')
-rw-r--r-- | client/app/users/shared/auth.service.ts | 41 |
1 files changed, 21 insertions, 20 deletions
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) { |