blob: 81c4f7de59ab4ef5cf8921a7e0f862e46c279d75 [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 Scandoloa4a47112016-12-16 10:06:13 -080019export interface IXosAuthService {
20 login(data: IAuthRequestData): Promise<any>;
21 logout(): Promise<any>;
Matteo Scandolod62ea792016-12-22 14:02:28 -080022 getUser(): any; // NOTE how to define return user || false ???
23 isAuthenticated(): boolean;
Matteo Scandolo0e363772017-01-13 11:41:29 -080024 clearUser(): void;
Matteo Scandoloa4a47112016-12-16 10:06:13 -080025}
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080026export class AuthService {
27
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080028 constructor(
29 private $http: angular.IHttpService,
30 private $q: angular.IQService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080031 private $cookies: angular.cookies.ICookiesService,
32 private AppConfig: IXosAppConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080033 ) {
34 }
35
36 public login(data: IAuthRequestData): Promise<any> {
37 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data)
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080039 .then((res: IAuthResponseData) => {
Matteo Scandoloebe5a222017-02-27 11:09:26 -080040 if (res.status >= 400) {
41 return d.reject(res.data);
42 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080043 this.$cookies.put('sessionid', res.data.sessionid, {path: '/'});
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044 d.resolve(res.data);
45 })
46 .catch(e => {
47 d.reject(e);
48 });
49 return d.promise;
50 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080051
52 public logout(): Promise<any> {
53 const d = this.$q.defer();
Matteo Scandolo1aee1982017-02-17 08:33:23 -080054 this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, {
55 // xoscsrftoken: this.$cookies.get('xoscsrftoken'),
56 // sessionid: this.$cookies.get('sessionid')
Matteo Scandoloa4a47112016-12-16 10:06:13 -080057 })
58 .then(() => {
Matteo Scandolo0e363772017-01-13 11:41:29 -080059 this.clearUser();
Matteo Scandoloa4a47112016-12-16 10:06:13 -080060 d.resolve();
61 })
62 .catch(e => {
63 d.reject(e);
64 });
65 return d.promise;
66 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080067
Matteo Scandolo0e363772017-01-13 11:41:29 -080068 public clearUser(): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080069 // this.$cookies.remove('xoscsrftoken', {path: '/'});
70 this.$cookies.remove('sessionid', {path: '/'});
71 // this.$cookies.remove('xosuser', {path: '/'});
Matteo Scandolo0e363772017-01-13 11:41:29 -080072 }
73
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080074 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -080075 const user = this.$cookies.get('xosuser');
76 if (angular.isDefined(user)) {
77 return JSON.parse(user);
78 }
79 return;
80 }
81
82 public isAuthenticated(): boolean {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080083 // const token = this.$cookies.get('xoscsrftoken');
84 const session = this.$cookies.get('sessionid');
85 return angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080086 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080087}