blob: 2dcabe92f02a6a8f867d1bae3dbf6f3cc2b953bf [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 Scandolo828d1e82017-01-17 14:49:38 -08007 static $inject = ['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(
12 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080013 private $state: angular.ui.IStateService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080014 private ModelSetup: IXosModelSetupService,
15 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080016 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080017
18 if (this.authService.isAuthenticated()) {
19 this.$state.go('xos.dashboard');
20 }
21
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080023
24 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080025 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080026 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080027 }
28
29 public login(username: string, password: string) {
30 this.authService.login({
31 username: username,
32 password: password
33 })
34 .then(res => {
Matteo Scandolob4b74a82017-01-12 13:12:26 -080035 // after login set up models
36 return this.ModelSetup.setup();
37 })
38 .then(() => {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080039 this.$state.go('xos.dashboard');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080040 })
41 .catch(e => console.error);
42 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080043
44 private getImg(img: string) {
45 return require(`../../images/brand/${img}`);
46 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080047}
48
49export const xosLogin: angular.IComponentOptions = {
50 template: require('./login.html'),
51 controllerAs: 'vm',
52 controller: LoginCtrl
53};