Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 1 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 2 | import {IXosAppConfig} from '../../../index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 3 | export interface IAuthRequestData { |
| 4 | username: string; |
| 5 | password: string; |
| 6 | } |
| 7 | |
| 8 | export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> { |
| 9 | data: { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 10 | sessionid: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 11 | }; |
| 12 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 13 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 14 | export interface IXosUser { |
| 15 | id: number; |
| 16 | email: string; |
| 17 | } |
| 18 | |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 19 | export interface IXosAuthService { |
| 20 | login(data: IAuthRequestData): Promise<any>; |
| 21 | logout(): Promise<any>; |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 22 | getUser(): any; // NOTE how to define return user || false ??? |
| 23 | isAuthenticated(): boolean; |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 24 | clearUser(): void; |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 25 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 26 | export class AuthService { |
| 27 | |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 28 | constructor( |
| 29 | private $http: angular.IHttpService, |
| 30 | private $q: angular.IQService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 31 | private $cookies: angular.cookies.ICookiesService, |
| 32 | private AppConfig: IXosAppConfig |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 33 | ) { |
| 34 | } |
| 35 | |
| 36 | public login(data: IAuthRequestData): Promise<any> { |
| 37 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 38 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/login`, data) |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 39 | .then((res: IAuthResponseData) => { |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 40 | if (res.status >= 400) { |
| 41 | return d.reject(res.data); |
| 42 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 43 | this.$cookies.put('sessionid', res.data.sessionid, {path: '/'}); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 44 | d.resolve(res.data); |
| 45 | }) |
| 46 | .catch(e => { |
| 47 | d.reject(e); |
| 48 | }); |
| 49 | return d.promise; |
| 50 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 51 | |
| 52 | public logout(): Promise<any> { |
| 53 | const d = this.$q.defer(); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 54 | this.$http.post(`${this.AppConfig.apiEndpoint}/utility/logout`, { |
| 55 | // xoscsrftoken: this.$cookies.get('xoscsrftoken'), |
| 56 | // sessionid: this.$cookies.get('sessionid') |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 57 | }) |
| 58 | .then(() => { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 59 | this.clearUser(); |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 60 | d.resolve(); |
| 61 | }) |
| 62 | .catch(e => { |
| 63 | d.reject(e); |
| 64 | }); |
| 65 | return d.promise; |
| 66 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 67 | |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 68 | public clearUser(): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 69 | // this.$cookies.remove('xoscsrftoken', {path: '/'}); |
| 70 | this.$cookies.remove('sessionid', {path: '/'}); |
| 71 | // this.$cookies.remove('xosuser', {path: '/'}); |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 74 | public getUser(): IXosUser { |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 75 | 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 Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 83 | // const token = this.$cookies.get('xoscsrftoken'); |
| 84 | const session = this.$cookies.get('sessionid'); |
| 85 | return angular.isDefined(session); |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 86 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 87 | } |