blob: b5b00fb229a1f037c2f6841427047b1c132cd576 [file] [log] [blame]
Matteo Scandolo43ffb672016-12-02 14:49:58 -08001/// <reference path="../../../../typings/index.d.ts"/>
2
3// Imports
4import {AppConfig} from '../../config/app.config';
5import {Injectable} from '@angular/core';
Matteo Scandolo0f77c502016-12-06 16:46:00 -08006import {Http, Response, Headers} from '@angular/http';
Matteo Scandolo43ffb672016-12-02 14:49:58 -08007import {Observable} from 'rxjs/Rx';
8import {IAuthRequest, IAuthResponse} from '../../interfaces/auth.interface';
9import {CookieService} from 'angular2-cookie/core';
10
11// Import RxJs required methods
12import 'rxjs/add/operator/map';
13import 'rxjs/add/operator/catch';
14
15@Injectable()
16export class AuthService {
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080017 private xosToken: string;
18 private xosSessionId: string;
19 // resolve HTTP using the constructor
20 constructor (private http: Http, private cookieService: CookieService) {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080021 }
22
23 // check if the user is authenticated
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080024 isAuthenticated(): string {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080025 this.xosToken = this.cookieService.get('xoscsrftoken');
26 this.xosSessionId = this.cookieService.get('xossessionid');
Matteo Scandolo0f77c502016-12-06 16:46:00 -080027 return this.xosToken && this.xosSessionId;
28 }
29
30 // get auth info to authenticate API
31 getUserHeaders(): Headers{
32 const headers = new Headers();
33 headers.append('x-csrftoken', this.cookieService.get('xoscsrftoken'));
34 headers.append('x-sessionid', this.cookieService.get('xossessionid'));
35 return headers;
Matteo Scandolo43ffb672016-12-02 14:49:58 -080036 }
37
38 // save cookies
Matteo Scandolo0f77c502016-12-06 16:46:00 -080039 private storeAuth(auth: IAuthResponse): void {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080040 this.cookieService.put('xoscsrftoken', auth.xoscsrftoken);
41 this.cookieService.put('xossessionid', auth.xossessionid);
42 }
43
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080044 // remove cookies
Matteo Scandolo0f77c502016-12-06 16:46:00 -080045 private removeAuth(): void {
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080046 this.cookieService.remove('xoscsrftoken');
47 this.cookieService.remove('xossessionid');
48 }
49
50 // log the user in
51 login(auth: IAuthRequest): Observable<IAuthResponse> {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080052 return this.http.post(`${AppConfig.apiEndpoint}/utility/login/`, auth)
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080053 .map((res: Response) => res.json())
54 .map((auth: IAuthResponse) => {
Matteo Scandolo43ffb672016-12-02 14:49:58 -080055 this.storeAuth(auth);
56 auth.user = JSON.parse(auth.user);
57 return auth;
58 })
59 .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
60 }
Matteo Scandolo8b9f1642016-12-05 17:08:26 -080061
62 // logout the user
63 logout(): Observable<any> {
64 return this.http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {xossessionid: this.xosSessionId})
65 .map((res: Response) => {
66 this.removeAuth();
67 return res.text();
68 })
69 .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
70 }
Matteo Scandolo43ffb672016-12-02 14:49:58 -080071}
72