blob: 520952d5a32d8af8128463f00b70fee6abc6cdd8 [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) => {
Matteo Scandolo6e5da162016-12-23 09:11:04 -080040 console.log(res.data);
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken);
42 this.$cookies.put('xossessionid', res.data.xossessionid);
43 this.$cookies.put('xosuser', res.data.user);
44 res.data.user = JSON.parse(res.data.user);
45 d.resolve(res.data);
46 })
47 .catch(e => {
48 d.reject(e);
49 });
50 return d.promise;
51 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080052
53 public logout(): Promise<any> {
54 const d = this.$q.defer();
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080055 this.$http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {
Matteo Scandoloa4a47112016-12-16 10:06:13 -080056 xoscsrftoken: this.$cookies.get('xoscsrftoken'),
57 xossessionid: this.$cookies.get('xossessionid')
58 })
59 .then(() => {
60 this.$cookies.remove('xoscsrftoken');
61 this.$cookies.remove('xossessionid');
62 this.$cookies.remove('xosuser');
63 d.resolve();
64 })
65 .catch(e => {
66 d.reject(e);
67 });
68 return d.promise;
69 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080070
71 public getUser(): IXosUser {
Matteo Scandolod62ea792016-12-22 14:02:28 -080072 const user = this.$cookies.get('xosuser');
73 if (angular.isDefined(user)) {
74 return JSON.parse(user);
75 }
76 return;
77 }
78
79 public isAuthenticated(): boolean {
80 const token = this.$cookies.get('xoscsrftoken');
81 const session = this.$cookies.get('xossessionid');
82 return angular.isDefined(token) && angular.isDefined(session);
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080083 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080084}