Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 1 | /// <reference path="../../../../typings/index.d.ts"/> |
| 2 | |
| 3 | // Imports |
| 4 | import {AppConfig} from '../../config/app.config'; |
| 5 | import {Injectable} from '@angular/core'; |
| 6 | import {Http, Response} from '@angular/http'; |
| 7 | import {Observable} from 'rxjs/Rx'; |
| 8 | import {IAuthRequest, IAuthResponse} from '../../interfaces/auth.interface'; |
| 9 | import {CookieService} from 'angular2-cookie/core'; |
| 10 | |
| 11 | // Import RxJs required methods |
| 12 | import 'rxjs/add/operator/map'; |
| 13 | import 'rxjs/add/operator/catch'; |
| 14 | |
| 15 | @Injectable() |
| 16 | export class AuthService { |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 17 | private xosToken: string; |
| 18 | private xosSessionId: string; |
| 19 | // resolve HTTP using the constructor |
| 20 | constructor (private http: Http, private cookieService: CookieService) { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // check if the user is authenticated |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 24 | isAuthenticated(): string { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 25 | this.xosToken = this.cookieService.get('xoscsrftoken'); |
| 26 | this.xosSessionId = this.cookieService.get('xossessionid'); |
| 27 | return this.xosToken; |
| 28 | } |
| 29 | |
| 30 | // save cookies |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 31 | storeAuth(auth: IAuthResponse): void { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 32 | this.cookieService.put('xoscsrftoken', auth.xoscsrftoken); |
| 33 | this.cookieService.put('xossessionid', auth.xossessionid); |
| 34 | } |
| 35 | |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 36 | // remove cookies |
| 37 | removeAuth(): void { |
| 38 | this.cookieService.remove('xoscsrftoken'); |
| 39 | this.cookieService.remove('xossessionid'); |
| 40 | } |
| 41 | |
| 42 | // log the user in |
| 43 | login(auth: IAuthRequest): Observable<IAuthResponse> { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 44 | return this.http.post(`${AppConfig.apiEndpoint}/utility/login/`, auth) |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 45 | .map((res: Response) => res.json()) |
| 46 | .map((auth: IAuthResponse) => { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 47 | this.storeAuth(auth); |
| 48 | auth.user = JSON.parse(auth.user); |
| 49 | return auth; |
| 50 | }) |
| 51 | .catch((error:any) => Observable.throw(error.json().error || 'Server error')); |
| 52 | } |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame^] | 53 | |
| 54 | // logout the user |
| 55 | logout(): Observable<any> { |
| 56 | return this.http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {xossessionid: this.xosSessionId}) |
| 57 | .map((res: Response) => { |
| 58 | this.removeAuth(); |
| 59 | return res.text(); |
| 60 | }) |
| 61 | .catch((error: any) => Observable.throw(error.json().error || 'Server error')); |
| 62 | } |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 63 | } |
| 64 | |