blob: 558fe37812a28e31262ce07a2ca356206c64f001 [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 Scandolo1aee1982017-02-17 08:33:23 -08005import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08006
7class LoginCtrl {
Matteo Scandolo1aee1982017-02-17 08:33:23 -08008 static $inject = ['$log', 'AuthService', '$state', 'XosModelDiscoverer', 'StyleConfig'];
Matteo Scandolo99fface2016-12-21 15:37:23 -08009 public loginStyle: any;
10 public img: string;
Matteo Scandoloebe5a222017-02-27 11:09:26 -080011 public showErrorMsg: boolean = false;
12 public errorMsg: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080013 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080014 private $log: ng.ILogService,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080016 private $state: angular.ui.IStateService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080017 private XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080018 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080019 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080020
21 if (this.authService.isAuthenticated()) {
22 this.$state.go('xos.dashboard');
23 }
24
Matteo Scandolo828d1e82017-01-17 14:49:38 -080025 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080026
27 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080028 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080029 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080030 }
31
32 public login(username: string, password: string) {
33 this.authService.login({
34 username: username,
35 password: password
36 })
37 .then(res => {
Matteo Scandoloebe5a222017-02-27 11:09:26 -080038 this.showErrorMsg = false;
Matteo Scandolob4b74a82017-01-12 13:12:26 -080039 // after login set up models
Matteo Scandolo1aee1982017-02-17 08:33:23 -080040 return this.XosModelDiscoverer.discover();
Matteo Scandolob4b74a82017-01-12 13:12:26 -080041 })
42 .then(() => {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080043 this.$state.go('xos.dashboard');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080044 })
Matteo Scandolo08464e52017-01-17 13:35:27 -080045 .catch(e => {
Matteo Scandoloebe5a222017-02-27 11:09:26 -080046 this.$log.error(`[XosLogin] Error during login.`);
47 this.errorMsg = `Something went wrong, please try again.`;
48 this.showErrorMsg = true;
Matteo Scandolo08464e52017-01-17 13:35:27 -080049 });
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080050 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080051
52 private getImg(img: string) {
53 return require(`../../images/brand/${img}`);
54 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080055}
56
57export const xosLogin: angular.IComponentOptions = {
58 template: require('./login.html'),
59 controllerAs: 'vm',
60 controller: LoginCtrl
61};