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) => { |
Matteo Scandolo | 6e5da16 | 2016-12-23 09:11:04 -0800 | [diff] [blame] | 40 | console.log(res.data); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 41 | this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken); |
| 42 | this.$cookies.put('xossessionid', res.data.xossessionid); |
| 43 | this.$cookies.put('xosuser', res.data.user); |
| 44 | res.data.user = JSON.parse(res.data.user); |
| 45 | d.resolve(res.data); |
| 46 | }) |
| 47 | .catch(e => { |
| 48 | d.reject(e); |
| 49 | }); |
| 50 | return d.promise; |
| 51 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 52 | |
| 53 | public logout(): Promise<any> { |
| 54 | const d = this.$q.defer(); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 55 | this.$http.post(`${AppConfig.apiEndpoint}/utility/logout/`, { |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 56 | xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 57 | xossessionid: this.$cookies.get('xossessionid') |
| 58 | }) |
| 59 | .then(() => { |
| 60 | this.$cookies.remove('xoscsrftoken'); |
| 61 | this.$cookies.remove('xossessionid'); |
| 62 | this.$cookies.remove('xosuser'); |
| 63 | d.resolve(); |
| 64 | }) |
| 65 | .catch(e => { |
| 66 | d.reject(e); |
| 67 | }); |
| 68 | return d.promise; |
| 69 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 70 | |
| 71 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 72 | const user = this.$cookies.get('xosuser'); |
| 73 | if (angular.isDefined(user)) { |
| 74 | return JSON.parse(user); |
| 75 | } |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | public isAuthenticated(): boolean { |
| 80 | const token = this.$cookies.get('xoscsrftoken'); |
| 81 | const session = this.$cookies.get('xossessionid'); |
| 82 | return angular.isDefined(token) && angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 83 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 84 | } |