]>
Commit | Line | Data |
---|---|---|
b1794c53 | 1 | import { Injectable } from 'angular2/core'; |
1553e15d | 2 | import { Http, Response, Headers, URLSearchParams, RequestOptions } from 'angular2/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 C |
13 | private _baseLoginUrl = '/api/v1/users/token'; |
14 | private _clientId = '56f055587305d40b21904240'; | |
15 | private _clientSecret = 'megustalabanana'; | |
16 | ||
1553e15d C |
17 | constructor (private http: Http) { |
18 | this._loginChanged = new Subject<AuthStatus>(); | |
19 | this.loginChanged$ = this._loginChanged.asObservable(); | |
20 | } | |
b1794c53 C |
21 | |
22 | login(username: string, password: string) { | |
23 | let body = new URLSearchParams(); | |
24 | body.set('client_id', this._clientId); | |
25 | body.set('client_secret', this._clientSecret); | |
26 | body.set('response_type', 'code'); | |
27 | body.set('grant_type', 'password'); | |
28 | body.set('scope', 'upload'); | |
29 | body.set('username', username); | |
30 | body.set('password', password); | |
31 | ||
32 | let headers = new Headers(); | |
33 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | |
34 | ||
35 | let options = { | |
36 | headers: headers | |
44124980 | 37 | }; |
b1794c53 C |
38 | |
39 | return this.http.post(this._baseLoginUrl, body.toString(), options) | |
40 | .map(res => res.json()) | |
41 | .catch(this.handleError); | |
42 | } | |
43 | ||
44 | logout() { | |
45 | // TODO make HTTP request | |
46 | } | |
47 | ||
1553e15d C |
48 | getRequestHeader(): Headers { |
49 | return new Headers({ 'Authorization': `${this.getTokenType()} ${this.getToken()}` }); | |
50 | } | |
51 | ||
52 | getAuthRequestOptions(): RequestOptions { | |
53 | return new RequestOptions({ headers: this.getRequestHeader() }); | |
54 | } | |
55 | ||
56 | getToken(): string { | |
57 | return localStorage.getItem('access_token'); | |
58 | } | |
59 | ||
60 | getTokenType(): string { | |
61 | return localStorage.getItem('token_type'); | |
62 | } | |
63 | ||
64 | getUser(): User { | |
65 | if (this.isLoggedIn() === false) { | |
66 | return null; | |
67 | } | |
68 | ||
69 | const user = User.load(); | |
70 | ||
71 | return user; | |
72 | } | |
73 | ||
74 | isLoggedIn(): boolean { | |
75 | if (this.getToken()) { | |
76 | return true; | |
77 | } else { | |
78 | return false; | |
79 | } | |
80 | } | |
81 | ||
b1794c53 C |
82 | setStatus(status: AuthStatus) { |
83 | this._loginChanged.next(status); | |
84 | } | |
85 | ||
86 | private handleError (error: Response) { | |
87 | console.error(error); | |
1553e15d | 88 | return Observable.throw(error.json() || { error: 'Server error' }); |
b1794c53 C |
89 | } |
90 | } |