diff options
Diffstat (limited to 'client/angular/users/services')
-rw-r--r-- | client/angular/users/services/auth.service.ts | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/client/angular/users/services/auth.service.ts b/client/angular/users/services/auth.service.ts index 80886346c..89412c3df 100644 --- a/client/angular/users/services/auth.service.ts +++ b/client/angular/users/services/auth.service.ts | |||
@@ -1,20 +1,23 @@ | |||
1 | import { Injectable } from 'angular2/core'; | 1 | import { Injectable } from 'angular2/core'; |
2 | import { Http, Response, Headers, URLSearchParams } from 'angular2/http'; | 2 | import { Http, Response, Headers, URLSearchParams, RequestOptions } from 'angular2/http'; |
3 | import { Observable, Subject } from 'rxjs/Rx'; | 3 | import { Observable, Subject } from 'rxjs/Rx'; |
4 | 4 | ||
5 | import { AuthStatus } from '../models/authStatus'; | 5 | import { AuthStatus } from '../models/authStatus'; |
6 | import { User } from '../models/user'; | ||
6 | 7 | ||
7 | @Injectable() | 8 | @Injectable() |
8 | export class AuthService { | 9 | export class AuthService { |
9 | loginChanged$ = this._loginChanged.asObservable(); | 10 | loginChanged$; |
10 | |||
11 | private _loginChanged = new Subject<AuthStatus>(); | ||
12 | 11 | ||
12 | private _loginChanged; | ||
13 | private _baseLoginUrl = '/api/v1/users/token'; | 13 | private _baseLoginUrl = '/api/v1/users/token'; |
14 | private _clientId = '56f055587305d40b21904240'; | 14 | private _clientId = '56f055587305d40b21904240'; |
15 | private _clientSecret = 'megustalabanana'; | 15 | private _clientSecret = 'megustalabanana'; |
16 | 16 | ||
17 | constructor (private http: Http) {} | 17 | constructor (private http: Http) { |
18 | this._loginChanged = new Subject<AuthStatus>(); | ||
19 | this.loginChanged$ = this._loginChanged.asObservable(); | ||
20 | } | ||
18 | 21 | ||
19 | login(username: string, password: string) { | 22 | login(username: string, password: string) { |
20 | let body = new URLSearchParams(); | 23 | let body = new URLSearchParams(); |
@@ -42,12 +45,46 @@ export class AuthService { | |||
42 | // TODO make HTTP request | 45 | // TODO make HTTP request |
43 | } | 46 | } |
44 | 47 | ||
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 | |||
45 | setStatus(status: AuthStatus) { | 82 | setStatus(status: AuthStatus) { |
46 | this._loginChanged.next(status); | 83 | this._loginChanged.next(status); |
47 | } | 84 | } |
48 | 85 | ||
49 | private handleError (error: Response) { | 86 | private handleError (error: Response) { |
50 | console.error(error); | 87 | console.error(error); |
51 | return Observable.throw(error.json().error || 'Server error'); | 88 | return Observable.throw(error.json() || { error: 'Server error' }); |
52 | } | 89 | } |
53 | } | 90 | } |