]>
Commit | Line | Data |
---|---|---|
b1794c53 C |
1 | import { Injectable } from 'angular2/core'; |
2 | import { Http, Response, Headers, URLSearchParams } from 'angular2/http'; | |
3 | import { Observable, Subject } from 'rxjs/Rx'; | |
4 | ||
b1794c53 C |
5 | import { AuthStatus } from '../models/authStatus'; |
6 | ||
7 | @Injectable() | |
8 | export class AuthService { | |
44124980 C |
9 | loginChanged$ = this._loginChanged.asObservable(); |
10 | ||
b1794c53 C |
11 | private _loginChanged = new Subject<AuthStatus>(); |
12 | ||
13 | private _baseLoginUrl = '/api/v1/users/token'; | |
14 | private _clientId = '56f055587305d40b21904240'; | |
15 | private _clientSecret = 'megustalabanana'; | |
16 | ||
b1794c53 C |
17 | constructor (private http: Http) {} |
18 | ||
19 | login(username: string, password: string) { | |
20 | let body = new URLSearchParams(); | |
21 | body.set('client_id', this._clientId); | |
22 | body.set('client_secret', this._clientSecret); | |
23 | body.set('response_type', 'code'); | |
24 | body.set('grant_type', 'password'); | |
25 | body.set('scope', 'upload'); | |
26 | body.set('username', username); | |
27 | body.set('password', password); | |
28 | ||
29 | let headers = new Headers(); | |
30 | headers.append('Content-Type', 'application/x-www-form-urlencoded'); | |
31 | ||
32 | let options = { | |
33 | headers: headers | |
44124980 | 34 | }; |
b1794c53 C |
35 | |
36 | return this.http.post(this._baseLoginUrl, body.toString(), options) | |
37 | .map(res => res.json()) | |
38 | .catch(this.handleError); | |
39 | } | |
40 | ||
41 | logout() { | |
42 | // TODO make HTTP request | |
43 | } | |
44 | ||
45 | setStatus(status: AuthStatus) { | |
46 | this._loginChanged.next(status); | |
47 | } | |
48 | ||
49 | private handleError (error: Response) { | |
50 | console.error(error); | |
51 | return Observable.throw(error.json().error || 'Server error'); | |
52 | } | |
53 | } |