blob: 8aa40d53676d8b29182aa89d4056a89aa6d6dcce [file] [log] [blame]
Matteo Scandolo035c5932016-12-14 09:55:15 -08001import {AuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo99fface2016-12-21 15:37:23 -08002import './login.scss';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08003
Matteo Scandolo828d1e82017-01-17 14:49:38 -08004import {IXosStyleConfig} from '../../../index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005
6class LoginCtrl {
Matteo Scandolo9b460042017-04-14 16:24:45 -07007 static $inject = [
8 '$log',
9 'AuthService',
10 '$state',
11 'StyleConfig'
12 ];
13
Matteo Scandolo99fface2016-12-21 15:37:23 -080014 public loginStyle: any;
15 public img: string;
Matteo Scandoloebe5a222017-02-27 11:09:26 -080016 public showErrorMsg: boolean = false;
17 public errorMsg: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080018 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080019 private $log: ng.ILogService,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080020 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080021 private $state: angular.ui.IStateService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080023 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080024
25 if (this.authService.isAuthenticated()) {
26 this.$state.go('xos.dashboard');
27 }
28
Matteo Scandolo828d1e82017-01-17 14:49:38 -080029 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080030
31 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080032 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080033 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080034 }
35
36 public login(username: string, password: string) {
37 this.authService.login({
38 username: username,
39 password: password
40 })
41 .then(res => {
Matteo Scandolo9b460042017-04-14 16:24:45 -070042 this.$state.go('loader');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080043 })
Matteo Scandolo08464e52017-01-17 13:35:27 -080044 .catch(e => {
Matteo Scandolo9b460042017-04-14 16:24:45 -070045 this.$log.error(`[XosLogin] Error during login.`, e);
Matteo Scandoloebe5a222017-02-27 11:09:26 -080046 this.errorMsg = `Something went wrong, please try again.`;
47 this.showErrorMsg = true;
Matteo Scandolo08464e52017-01-17 13:35:27 -080048 });
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080049 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080050
51 private getImg(img: string) {
52 return require(`../../images/brand/${img}`);
53 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080054}
55
56export const xosLogin: angular.IComponentOptions = {
57 template: require('./login.html'),
58 controllerAs: 'vm',
59 controller: LoginCtrl
60};