Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 2 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 3 | export interface IAuthRequestData { |
| 4 | username: string; |
| 5 | password: string; |
| 6 | } |
| 7 | |
| 8 | export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> { |
| 9 | data: { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 10 | // user: string; |
| 11 | // xoscsrftoken: string; |
| 12 | sessionid: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 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 | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 26 | clearUser(): void; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 27 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 28 | export class AuthService { |
| 29 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 30 | constructor( |
| 31 | private $http: angular.IHttpService, |
| 32 | private $q: angular.IQService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 33 | private $cookies: angular.cookies.ICookiesService, |
| 34 | private AppConfig: IXosAppConfig |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 35 | ) { |
| 36 | } |
| 37 | |
| 38 | public login(data: IAuthRequestData): Promise<any> { |
| 39 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 40 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 41 | .then((res: IAuthResponseData) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 42 | // this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken, {path: '/'}); |
| 43 | this.$cookies.put('sessionid', res.data.sessionid, {path: '/'}); |
| 44 | // this.$cookies.put('xosuser', res.data.user, {path: '/'}); |
| 45 | // res.data.user = JSON.parse(res.data.user); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 46 | d.resolve(res.data); |
| 47 | }) |
| 48 | .catch(e => { |
| 49 | d.reject(e); |
| 50 | }); |
| 51 | return d.promise; |
| 52 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 53 | |
| 54 | public logout(): Promise<any> { |
| 55 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 56 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, { |
| 57 | // xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 58 | // sessionid: this.$cookies.get('sessionid') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 59 | }) |
| 60 | .then(() => { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 61 | this.clearUser(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 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 | |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 70 | public clearUser(): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 71 | // this.$cookies.remove('xoscsrftoken', {path: '/'}); |
| 72 | this.$cookies.remove('sessionid', {path: '/'}); |
| 73 | // this.$cookies.remove('xosuser', {path: '/'}); |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 76 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 77 | const user = this.$cookies.get('xosuser'); |
| 78 | if (angular.isDefined(user)) { |
| 79 | return JSON.parse(user); |
| 80 | } |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | public isAuthenticated(): boolean { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame^] | 85 | // const token = this.$cookies.get('xoscsrftoken'); |
| 86 | const session = this.$cookies.get('sessionid'); |
| 87 | return angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 88 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 89 | } |