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'; |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 5 | import {Injectable} from '@angular/core'; |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 6 | import {Http, Response, Headers} from '@angular/http'; |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 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'); |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 27 | return this.xosToken && this.xosSessionId; |
| 28 | } |
| 29 | |
| 30 | // get auth info to authenticate API |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 31 | getUserHeaders(): Headers { |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 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 Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Matteo Scandolo | 40f8fa9 | 2016-12-07 09:21:35 -0800 | [diff] [blame] | 38 | // log the user in |
| 39 | login(auth: IAuthRequest): Observable<IAuthResponse> { |
| 40 | return this.http.post(`${AppConfig.apiEndpoint}/utility/login/`, auth) |
| 41 | .map((res: Response) => res.json()) |
| 42 | .map((auth: IAuthResponse) => { |
| 43 | this.storeAuth(auth); |
| 44 | auth.user = JSON.parse(auth.user); |
| 45 | return auth; |
| 46 | }) |
| 47 | .catch((error: any) => Observable.throw(error.json().error || 'Server error')); |
| 48 | } |
| 49 | |
| 50 | // logout the user |
| 51 | logout(): Observable<any> { |
| 52 | return this.http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {xossessionid: this.xosSessionId}) |
| 53 | .map((res: Response) => { |
| 54 | this.removeAuth(); |
| 55 | return res.text(); |
| 56 | }) |
| 57 | .catch((error: any) => Observable.throw(error.json().error || 'Server error')); |
| 58 | } |
| 59 | |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 60 | // save cookies |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 61 | private storeAuth(auth: IAuthResponse): void { |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 62 | this.cookieService.put('xoscsrftoken', auth.xoscsrftoken); |
| 63 | this.cookieService.put('xossessionid', auth.xossessionid); |
| 64 | } |
| 65 | |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame] | 66 | // remove cookies |
Matteo Scandolo | 0f77c50 | 2016-12-06 16:46:00 -0800 | [diff] [blame] | 67 | private removeAuth(): void { |
Matteo Scandolo | 8b9f164 | 2016-12-05 17:08:26 -0800 | [diff] [blame] | 68 | this.cookieService.remove('xoscsrftoken'); |
| 69 | this.cookieService.remove('xossessionid'); |
| 70 | } |
Matteo Scandolo | 43ffb67 | 2016-12-02 14:49:58 -0800 | [diff] [blame] | 71 | } |
| 72 | |