aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/angular/users/services/auth.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/angular/users/services/auth.service.ts')
-rw-r--r--client/angular/users/services/auth.service.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/client/angular/users/services/auth.service.ts b/client/angular/users/services/auth.service.ts
new file mode 100644
index 000000000..a512c3d9c
--- /dev/null
+++ b/client/angular/users/services/auth.service.ts
@@ -0,0 +1,54 @@
1import { Injectable } from 'angular2/core';
2import { Http, Response, Headers, URLSearchParams } from 'angular2/http';
3import { Observable, Subject } from 'rxjs/Rx';
4
5import { Token } from '../models/token';
6import { AuthStatus } from '../models/authStatus';
7
8@Injectable()
9export class AuthService {
10 private _loginChanged = new Subject<AuthStatus>();
11
12 private _baseLoginUrl = '/api/v1/users/token';
13 private _clientId = '56f055587305d40b21904240';
14 private _clientSecret = 'megustalabanana';
15
16 loginChanged$ = this._loginChanged.asObservable();
17
18 constructor (private http: Http) {}
19
20 login(username: string, password: string) {
21 let body = new URLSearchParams();
22 body.set('client_id', this._clientId);
23 body.set('client_secret', this._clientSecret);
24 body.set('response_type', 'code');
25 body.set('grant_type', 'password');
26 body.set('scope', 'upload');
27 body.set('username', username);
28 body.set('password', password);
29
30 let headers = new Headers();
31 headers.append('Content-Type', 'application/x-www-form-urlencoded');
32
33 let options = {
34 headers: headers
35 }
36
37 return this.http.post(this._baseLoginUrl, body.toString(), options)
38 .map(res => res.json())
39 .catch(this.handleError);
40 }
41
42 logout() {
43 // TODO make HTTP request
44 }
45
46 setStatus(status: AuthStatus) {
47 this._loginChanged.next(status);
48 }
49
50 private handleError (error: Response) {
51 console.error(error);
52 return Observable.throw(error.json().error || 'Server error');
53 }
54}