Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import {AppConfig} from '../../config/app.config'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 2 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
| 3 | export interface IAuthRequestData { |
| 4 | username: string; |
| 5 | password: string; |
| 6 | } |
| 7 | |
| 8 | export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> { |
| 9 | data: { |
| 10 | user: string; |
| 11 | xoscsrftoken: string; |
| 12 | xossessionid: string; |
| 13 | }; |
| 14 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 15 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 16 | export interface IXosUser { |
| 17 | id: number; |
| 18 | email: string; |
| 19 | } |
| 20 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 21 | export interface IXosAuthService { |
| 22 | login(data: IAuthRequestData): Promise<any>; |
| 23 | logout(): Promise<any>; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 24 | getUser(): any; // NOTE how to define return user || false ??? |
| 25 | isAuthenticated(): boolean; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 26 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 27 | export class AuthService { |
| 28 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 29 | constructor( |
| 30 | private $http: angular.IHttpService, |
| 31 | private $q: angular.IQService, |
| 32 | private $cookies: angular.cookies.ICookiesService |
| 33 | ) { |
| 34 | } |
| 35 | |
| 36 | public login(data: IAuthRequestData): Promise<any> { |
| 37 | const d = this.$q.defer(); |
| 38 | this.$http.post(`${AppConfig.apiEndpoint}/utility/login/`, data) |
| 39 | .then((res: IAuthResponseData) => { |
| 40 | this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken); |
| 41 | this.$cookies.put('xossessionid', res.data.xossessionid); |
| 42 | this.$cookies.put('xosuser', res.data.user); |
| 43 | res.data.user = JSON.parse(res.data.user); |
| 44 | d.resolve(res.data); |
| 45 | }) |
| 46 | .catch(e => { |
| 47 | d.reject(e); |
| 48 | }); |
| 49 | return d.promise; |
| 50 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 51 | |
| 52 | public logout(): Promise<any> { |
| 53 | const d = this.$q.defer(); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 54 | this.$http.post(`${AppConfig.apiEndpoint}/utility/logout/`, { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 55 | xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 56 | xossessionid: this.$cookies.get('xossessionid') |
| 57 | }) |
| 58 | .then(() => { |
| 59 | this.$cookies.remove('xoscsrftoken'); |
| 60 | this.$cookies.remove('xossessionid'); |
| 61 | this.$cookies.remove('xosuser'); |
| 62 | d.resolve(); |
| 63 | }) |
| 64 | .catch(e => { |
| 65 | d.reject(e); |
| 66 | }); |
| 67 | return d.promise; |
| 68 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 69 | |
| 70 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 71 | const user = this.$cookies.get('xosuser'); |
| 72 | if (angular.isDefined(user)) { |
| 73 | return JSON.parse(user); |
| 74 | } |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | public isAuthenticated(): boolean { |
| 79 | const token = this.$cookies.get('xoscsrftoken'); |
| 80 | const session = this.$cookies.get('xossessionid'); |
| 81 | return angular.isDefined(token) && angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 82 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 83 | } |