blob: f963799cfec70125fc5667e1f6075675bb11b420 [file] [log] [blame]
Matteo Scandolo035c5932016-12-14 09:55:15 -08001import {AppConfig} from '../../config/app.config';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08002import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg;
3export 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 Scandoloa4a47112016-12-16 10:06:13 -080026}
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080027export class AuthService {
28
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080029 constructor(
30 private $http: angular.IHttpService,
31 private $q: angular.IQService,
32 private $cookies: angular.cookies.ICookiesService
33 ) {
34 }
35
36 public login(data: IAuthRequestData): Promise<any> {
37 const d = this.$q.defer();
38 this.$http.post(`${AppConfig.apiEndpoint}/utility/login/`, data)
39 .then((res: IAuthResponseData) => {
40 this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken);
41 this.$cookies.put('xossessionid', res.data.xossessionid);
42 this.$cookies.put('xosuser', res.data.user);
43 res.data.user = JSON.parse(res.data.user);
44 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 Scandoloa8a6fbb2016-12-21 16:59:08 -080054 this.$http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080055 xoscsrftoken: this.$cookies.get('xoscsrftoken'),
56 xossessionid: this.$cookies.get('xossessionid')
57 })
58 .then(() => {
59 this.$cookies.remove('xoscsrftoken');
60 this.$cookies.remove('xossessionid');
61 this.$cookies.remove('xosuser');
62 d.resolve();
63 })
64 .catch(e => {
65 d.reject(e);
66 });
67 return d.promise;
68 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080069
70 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -080071 const user = this.$cookies.get('xosuser');
72 if (angular.isDefined(user)) {
73 return JSON.parse(user);
74 }
75 return;
76 }
77
78 public isAuthenticated(): boolean {
79 const token = this.$cookies.get('xoscsrftoken');
80 const session = this.$cookies.get('xossessionid');
81 return angular.isDefined(token) && angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080082 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080083}