blob: edc2a657b6c11959764cf1179351d781648b3038 [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 // user: string;
11 // xoscsrftoken: string;
12 sessionid: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080013 };
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 Scandolo1aee1982017-02-17 08:33:23 -080040 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 .then((res: IAuthResponseData) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080042 // 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 Scandolof6acdbe2016-12-13 10:29:37 -080046 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 Scandolo1aee1982017-02-17 08:33:23 -080056 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, {
57 // xoscsrftoken: this.$cookies.get('xoscsrftoken'),
58 // sessionid: this.$cookies.get('sessionid')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080059 })
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 Scandolo1aee1982017-02-17 08:33:23 -080071 // this.$cookies.remove('xoscsrftoken', {path: '/'});
72 this.$cookies.remove('sessionid', {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 {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080085 // const token = this.$cookies.get('xoscsrftoken');
86 const session = this.$cookies.get('sessionid');
87 return angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080088 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080089}