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