blob: 183980a536a9f4b58e1194ad76ec2875655bbd2c [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 Scandolo1aee1982017-02-17 08:33:23 -080011
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080012 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080013 private $log: ng.ILogService,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080014 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080015 private $state: angular.ui.IStateService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080016 private XosModelDiscoverer: IXosModelDiscovererService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080017 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080018 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080019
20 if (this.authService.isAuthenticated()) {
21 this.$state.go('xos.dashboard');
22 }
23
Matteo Scandolo828d1e82017-01-17 14:49:38 -080024 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080025
26 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080027 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080028 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080029 }
30
31 public login(username: string, password: string) {
32 this.authService.login({
33 username: username,
34 password: password
35 })
36 .then(res => {
Matteo Scandolob4b74a82017-01-12 13:12:26 -080037 // after login set up models
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 return this.XosModelDiscoverer.discover();
Matteo Scandolob4b74a82017-01-12 13:12:26 -080039 })
40 .then(() => {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080041 this.$state.go('xos.dashboard');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080042 })
Matteo Scandolo08464e52017-01-17 13:35:27 -080043 .catch(e => {
Matteo Scandolo17bf8242017-01-23 17:30:39 -080044 this.$log.error(e);
Matteo Scandolo08464e52017-01-17 13:35:27 -080045 });
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080047
48 private getImg(img: string) {
49 return require(`../../images/brand/${img}`);
50 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080051}
52
53export const xosLogin: angular.IComponentOptions = {
54 template: require('./login.html'),
55 controllerAs: 'vm',
56 controller: LoginCtrl
57};