Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import {AppConfig} from '../../config/app.config'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 2 | import IHttpPromiseCallbackArg = angular.IHttpPromiseCallbackArg; |
| 3 | export interface IAuthRequestData { |
| 4 | username: string; |
| 5 | password: string; |
| 6 | } |
| 7 | |
| 8 | export interface IAuthResponseData extends IHttpPromiseCallbackArg<any> { |
| 9 | data: { |
| 10 | user: string; |
| 11 | xoscsrftoken: string; |
| 12 | xossessionid: string; |
| 13 | }; |
| 14 | } |
| 15 | export class AuthService { |
| 16 | |
| 17 | |
| 18 | /** @ngInject */ |
| 19 | constructor( |
| 20 | private $http: angular.IHttpService, |
| 21 | private $q: angular.IQService, |
| 22 | private $cookies: angular.cookies.ICookiesService |
| 23 | ) { |
| 24 | } |
| 25 | |
| 26 | public login(data: IAuthRequestData): Promise<any> { |
| 27 | const d = this.$q.defer(); |
| 28 | this.$http.post(`${AppConfig.apiEndpoint}/utility/login/`, data) |
| 29 | .then((res: IAuthResponseData) => { |
| 30 | this.$cookies.put('xoscsrftoken', res.data.xoscsrftoken); |
| 31 | this.$cookies.put('xossessionid', res.data.xossessionid); |
| 32 | this.$cookies.put('xosuser', res.data.user); |
| 33 | res.data.user = JSON.parse(res.data.user); |
| 34 | d.resolve(res.data); |
| 35 | }) |
| 36 | .catch(e => { |
| 37 | d.reject(e); |
| 38 | }); |
| 39 | return d.promise; |
| 40 | } |
| 41 | } |