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