blob: 2979bf16e1d95b3cb9c9fb5b365ab2f7c6cee2ad [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: {
10 user: string;
11 xoscsrftoken: string;
12 xossessionid: string;
13 };
14}
Matteo Scandoloa4a47112016-12-16 10:06:13 -080015
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080016export interface IXosUser {
17 id: number;
18 email: string;
19}
20
Matteo Scandoloa4a47112016-12-16 10:06:13 -080021export interface IXosAuthService {
22 login(data: IAuthRequestData): Promise<any>;
23 logout(): Promise<any>;
Matteo Scandolod62ea792016-12-22 14:02:28 -080024 getUser(): any; // NOTE how to define return user || false ???
25 isAuthenticated(): boolean;
Matteo Scandolo0e363772017-01-13 11:41:29 -080026 clearUser(): void;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080027}
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080028export class AuthService {
29
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080030 constructor(
31 private $http: angular.IHttpService,
32 private $q: angular.IQService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080033 private $cookies: angular.cookies.ICookiesService,
34 private AppConfig: IXosAppConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080035 ) {
36 }
37
38 public login(data: IAuthRequestData): Promise<any> {
39 const d = this.$q.defer();
Matteo Scandolo828d1e82017-01-17 14:49:38 -080040 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login/`, data)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 .then((res: IAuthResponseData) => {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080042 this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken, {path: '/'});
43 this.$cookies.put('xossessionid', res.data.xossessionid, {path: '/'});
44 this.$cookies.put('xosuser', res.data.user, {path: '/'});
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080045 res.data.user = JSON.parse(res.data.user);
46 d.resolve(res.data);
47 })
48 .catch(e => {
49 d.reject(e);
50 });
51 return d.promise;
52 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080053
54 public logout(): Promise<any> {
55 const d = this.$q.defer();
Matteo Scandolo828d1e82017-01-17 14:49:38 -080056 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout/`, {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080057 xoscsrftoken: this.$cookies.get('xoscsrftoken'),
58 xossessionid: this.$cookies.get('xossessionid')
59 })
60 .then(() => {
Matteo Scandolo0e363772017-01-13 11:41:29 -080061 this.clearUser();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080062 d.resolve();
63 })
64 .catch(e => {
65 d.reject(e);
66 });
67 return d.promise;
68 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080069
Matteo Scandolo0e363772017-01-13 11:41:29 -080070 public clearUser(): void {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080071 this.$cookies.remove('xoscsrftoken', {path: '/'});
72 this.$cookies.remove('xossessionid', {path: '/'});
73 this.$cookies.remove('xosuser', {path: '/'});
Matteo Scandolo0e363772017-01-13 11:41:29 -080074 }
75
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080076 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -080077 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 {
85 const token = this.$cookies.get('xoscsrftoken');
86 const session = this.$cookies.get('xossessionid');
87 return angular.isDefined(token) && angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080088 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080089}