blob: 673d9a854812d8839d950b7a89840924665c141a [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 Scandolob4b74a82017-01-12 13:12:26 -08003import {IXosModelSetupService} from '../services/helpers/model-setup.helpers';
Matteo Scandolo828d1e82017-01-17 14:49:38 -08004import {IXosStyleConfig} from '../../../index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005
6class LoginCtrl {
Matteo Scandolo17bf8242017-01-23 17:30:39 -08007 static $inject = ['$log', 'AuthService', '$state', 'ModelSetup', 'StyleConfig'];
Matteo Scandolo99fface2016-12-21 15:37:23 -08008 public loginStyle: any;
9 public img: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080010 /** @ngInject */
11 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080012 private $log: ng.ILogService,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080013 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080014 private $state: angular.ui.IStateService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080015 private ModelSetup: IXosModelSetupService,
16 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080017 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080018
19 if (this.authService.isAuthenticated()) {
20 this.$state.go('xos.dashboard');
21 }
22
Matteo Scandolo828d1e82017-01-17 14:49:38 -080023 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080024
25 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080026 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080027 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080028 }
29
30 public login(username: string, password: string) {
31 this.authService.login({
32 username: username,
33 password: password
34 })
35 .then(res => {
Matteo Scandolob4b74a82017-01-12 13:12:26 -080036 // after login set up models
37 return this.ModelSetup.setup();
38 })
39 .then(() => {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080040 this.$state.go('xos.dashboard');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 })
Matteo Scandolo08464e52017-01-17 13:35:27 -080042 .catch(e => {
Matteo Scandolo17bf8242017-01-23 17:30:39 -080043 this.$log.error(e);
Matteo Scandolo08464e52017-01-17 13:35:27 -080044 });
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080045 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080046
47 private getImg(img: string) {
48 return require(`../../images/brand/${img}`);
49 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080050}
51
52export const xosLogin: angular.IComponentOptions = {
53 template: require('./login.html'),
54 controllerAs: 'vm',
55 controller: LoginCtrl
56};