blob: a94599a4de8d19e652e6cc9876c986e130acf360 [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}
15export 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}