blob: 82f71b6c3bf436a6b6a02506b6967e3c96273d1b [file] [log] [blame]
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08001import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg;
Matteo Scandolo828d1e82017-01-17 14:49:38 -08002import {IXosAppConfig} from '../../../index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08003export interface IAuthRequestData {
4 username: string;
5 password: string;
6}
7
8export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> {
9 data: {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080010 sessionid: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080011 };
12}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080013
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080014export interface IXosUser {
15 id: number;
16 email: string;
17}
18
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070019export interface IXosRestError {
20 error: string;
21 specific_error: string;
22 fields: any;
23}
24
Matteo Scandoloa4a47112016-12-16 10:06:13 -080025export interface IXosAuthService {
26 login(data: IAuthRequestData): Promise<any>;
27 logout(): Promise<any>;
Matteo Scandolod62ea792016-12-22 14:02:28 -080028 getUser(): any; // NOTE how to define return user || false ???
29 isAuthenticated(): boolean;
Matteo Scandolo0e363772017-01-13 11:41:29 -080030 clearUser(): void;
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070031 handleUnauthenticatedRequest(error: IXosRestError | string): void;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080032}
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080033export class AuthService {
34
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035 constructor(
36 private $http: angular.IHttpService,
37 private $q: angular.IQService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080038 private $cookies: angular.cookies.ICookiesService,
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070039 private AppConfig: IXosAppConfig,
40 private $state: angular.ui.IStateService
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 ) {
42 }
43
44 public login(data: IAuthRequestData): Promise<any> {
45 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080046 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080047 .then((res: IAuthResponseData) => {
Matteo Scandoloebe5a222017-02-27 11:09:26 -080048 if (res.status >= 400) {
49 return d.reject(res.data);
50 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080051 this.$cookies.put('sessionid', res.data.sessionid, {path: '/'});
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080052 d.resolve(res.data);
53 })
54 .catch(e => {
55 d.reject(e);
56 });
57 return d.promise;
58 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080059
60 public logout(): Promise<any> {
61 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080062 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, {
63 // xoscsrftoken: this.$cookies.get('xoscsrftoken'),
64 // sessionid: this.$cookies.get('sessionid')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080065 })
66 .then(() => {
Matteo Scandolo0e363772017-01-13 11:41:29 -080067 this.clearUser();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080068 d.resolve();
69 })
70 .catch(e => {
71 d.reject(e);
72 });
73 return d.promise;
74 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080075
Matteo Scandolo0e363772017-01-13 11:41:29 -080076 public clearUser(): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080077 // this.$cookies.remove('xoscsrftoken', {path: '/'});
78 this.$cookies.remove('sessionid', {path: '/'});
79 // this.$cookies.remove('xosuser', {path: '/'});
Matteo Scandolo0e363772017-01-13 11:41:29 -080080 }
81
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080082 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -080083 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 Scandolo1aee1982017-02-17 08:33:23 -080091 // const token = this.$cookies.get('xoscsrftoken');
92 const session = this.$cookies.get('sessionid');
93 return angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080094 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070095
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 Scandolof6acdbe2016-12-13 10:29:37 -0800120}