diff options
Diffstat (limited to 'client/app/shared/users/auth.service.ts')
-rw-r--r-- | client/app/shared/users/auth.service.ts | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/client/app/shared/users/auth.service.ts b/client/app/shared/users/auth.service.ts new file mode 100644 index 000000000..d63fe38f3 --- /dev/null +++ b/client/app/shared/users/auth.service.ts | |||
@@ -0,0 +1,108 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { Headers, Http, RequestOptions, Response, URLSearchParams } from '@angular/http'; | ||
3 | import { Observable, Subject } from 'rxjs/Rx'; | ||
4 | |||
5 | import { AuthStatus } from './auth-status.model'; | ||
6 | import { User } from './user.model'; | ||
7 | |||
8 | @Injectable() | ||
9 | export class AuthService { | ||
10 | private static BASE_CLIENT_URL = '/api/v1/users/client'; | ||
11 | private static BASE_LOGIN_URL = '/api/v1/users/token'; | ||
12 | |||
13 | loginChangedSource: Observable<AuthStatus>; | ||
14 | |||
15 | private clientId: string; | ||
16 | private clientSecret: string; | ||
17 | private loginChanged: Subject<AuthStatus>; | ||
18 | |||
19 | constructor(private http: Http) { | ||
20 | this.loginChanged = new Subject<AuthStatus>(); | ||
21 | this.loginChangedSource = this.loginChanged.asObservable(); | ||
22 | |||
23 | // Fetch the client_id/client_secret | ||
24 | // FIXME: save in local storage? | ||
25 | this.http.get(AuthService.BASE_CLIENT_URL) | ||
26 | .map(res => res.json()) | ||
27 | .catch(this.handleError) | ||
28 | .subscribe( | ||
29 | result => { | ||
30 | this.clientId = result.client_id; | ||
31 | this.clientSecret = result.client_secret; | ||
32 | console.log('Client credentials loaded.'); | ||
33 | }, | ||
34 | error => { | ||
35 | alert(error); | ||
36 | } | ||
37 | ); | ||
38 | } | ||
39 | |||
40 | getAuthRequestOptions(): RequestOptions { | ||
41 | return new RequestOptions({ headers: this.getRequestHeader() }); | ||
42 | } | ||
43 | |||
44 | getRequestHeader() { | ||
45 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | ||
46 | } | ||
47 | |||
48 | getToken() { | ||
49 | return localStorage.getItem('access_token'); | ||
50 | } | ||
51 | |||
52 | getTokenType() { | ||
53 | return localStorage.getItem('token_type'); | ||
54 | } | ||
55 | |||
56 | getUser(): User { | ||
57 | if (this.isLoggedIn() === false) { | ||
58 | return null; | ||
59 | } | ||
60 | |||
61 | const user = User.load(); | ||
62 | |||
63 | return user; | ||
64 | } | ||
65 | |||
66 | isLoggedIn() { | ||
67 | if (this.getToken()) { | ||
68 | return true; | ||
69 | } else { | ||
70 | return false; | ||
71 | } | ||
72 | } | ||
73 | |||
74 | login(username: string, password: string) { | ||
75 | let body = new URLSearchParams(); | ||
76 | body.set('client_id', this.clientId); | ||
77 | body.set('client_secret', this.clientSecret); | ||
78 | body.set('response_type', 'code'); | ||
79 | body.set('grant_type', 'password'); | ||
80 | body.set('scope', 'upload'); | ||
81 | body.set('username', username); | ||
82 | body.set('password', password); | ||
83 | |||
84 | let headers = new Headers(); | ||
85 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
86 | |||
87 | let options = { | ||
88 | headers: headers | ||
89 | }; | ||
90 | |||
91 | return this.http.post(AuthService.BASE_LOGIN_URL, body.toString(), options) | ||
92 | .map(res => res.json()) | ||
93 | .catch(this.handleError); | ||
94 | } | ||
95 | |||
96 | logout() { | ||
97 | // TODO make HTTP request | ||
98 | } | ||
99 | |||
100 | setStatus(status: AuthStatus) { | ||
101 | this.loginChanged.next(status); | ||
102 | } | ||
103 | |||
104 | private handleError (error: Response) { | ||
105 | console.error(error); | ||
106 | return Observable.throw(error.json() || { error: 'Server error' }); | ||
107 | } | ||
108 | } | ||