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 | sessionid: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 11 | }; |
| 12 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 13 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 14 | export interface IXosUser { |
| 15 | id: number; |
| 16 | email: string; |
| 17 | } |
| 18 | |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame^] | 19 | export interface IXosRestError { |
| 20 | error: string; |
| 21 | specific_error: string; |
| 22 | fields: any; |
| 23 | } |
| 24 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 25 | export interface IXosAuthService { |
| 26 | login(data: IAuthRequestData): Promise<any>; |
| 27 | logout(): Promise<any>; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 28 | getUser(): any; // NOTE how to define return user || false ??? |
| 29 | isAuthenticated(): boolean; |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 30 | clearUser(): void; |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame^] | 31 | handleUnauthenticatedRequest(error: IXosRestError | string): void; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 32 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 33 | export class AuthService { |
| 34 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 35 | constructor( |
| 36 | private $http: angular.IHttpService, |
| 37 | private $q: angular.IQService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 38 | private $cookies: angular.cookies.ICookiesService, |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame^] | 39 | private AppConfig: IXosAppConfig, |
| 40 | private $state: angular.ui.IStateService |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 41 | ) { |
| 42 | } |
| 43 | |
| 44 | public login(data: IAuthRequestData): Promise<any> { |
| 45 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 46 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 47 | .then((res: IAuthResponseData) => { |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 48 | if (res.status >= 400) { |
| 49 | return d.reject(res.data); |
| 50 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 51 | this.$cookies.put('sessionid', res.data.sessionid, {path: '/'}); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 52 | d.resolve(res.data); |
| 53 | }) |
| 54 | .catch(e => { |
| 55 | d.reject(e); |
| 56 | }); |
| 57 | return d.promise; |
| 58 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 59 | |
| 60 | public logout(): Promise<any> { |
| 61 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 62 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, { |
| 63 | // xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 64 | // sessionid: this.$cookies.get('sessionid') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 65 | }) |
| 66 | .then(() => { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 67 | this.clearUser(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 68 | d.resolve(); |
| 69 | }) |
| 70 | .catch(e => { |
| 71 | d.reject(e); |
| 72 | }); |
| 73 | return d.promise; |
| 74 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 75 | |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 76 | public clearUser(): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 77 | // this.$cookies.remove('xoscsrftoken', {path: '/'}); |
| 78 | this.$cookies.remove('sessionid', {path: '/'}); |
| 79 | // this.$cookies.remove('xosuser', {path: '/'}); |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 82 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 83 | const user = this.$cookies.get('xosuser'); |
| 84 | if (angular.isDefined(user)) { |
| 85 | return JSON.parse(user); |
| 86 | } |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | public isAuthenticated(): boolean { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 91 | // const token = this.$cookies.get('xoscsrftoken'); |
| 92 | const session = this.$cookies.get('sessionid'); |
| 93 | return angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 94 | } |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame^] | 95 | |
| 96 | public handleUnauthenticatedRequest(res: IXosRestError | string): void { |
| 97 | let err; |
| 98 | if (angular.isString(res)) { |
| 99 | try { |
| 100 | err = JSON.parse(res); |
| 101 | } catch (e) { |
| 102 | // NOTE if it's not JSON it means that is not the error we're handling here |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (angular.isObject(res)) { |
| 108 | err = res; |
| 109 | } |
| 110 | |
| 111 | if (err && err.error) { |
| 112 | switch (err.error) { |
| 113 | case 'XOSPermissionDenied': |
| 114 | this.clearUser(); |
| 115 | this.$state.go('login'); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 120 | } |