blob: ac0da8262653a8e42e166731394360bf086ba566 [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 {StyleConfig} from '../../config/style.config';
3import './login.scss';
Matteo Scandolob4b74a82017-01-12 13:12:26 -08004import {IXosModelSetupService} from '../services/helpers/model-setup.helpers';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -08005
6class LoginCtrl {
Matteo Scandolob4b74a82017-01-12 13:12:26 -08007 static $inject = ['AuthService', '$state', 'ModelSetup'];
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,
14 private ModelSetup: IXosModelSetupService
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080015 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080016
17 if (this.authService.isAuthenticated()) {
18 this.$state.go('xos.dashboard');
19 }
20
Matteo Scandolo99fface2016-12-21 15:37:23 -080021 this.img = this.getImg(StyleConfig.background);
22
23 this.loginStyle = {
24 'background-image': `url('${this.getImg(StyleConfig.background)}')`
25 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080026 }
27
28 public login(username: string, password: string) {
29 this.authService.login({
30 username: username,
31 password: password
32 })
33 .then(res => {
Matteo Scandolob4b74a82017-01-12 13:12:26 -080034 // after login set up models
35 return this.ModelSetup.setup();
36 })
37 .then(() => {
Matteo Scandolo9f87f302016-12-13 18:11:10 -080038 this.$state.go('xos.dashboard');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080039 })
40 .catch(e => console.error);
41 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080042
43 private getImg(img: string) {
44 return require(`../../images/brand/${img}`);
45 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080046}
47
48export const xosLogin: angular.IComponentOptions = {
49 template: require('./login.html'),
50 controllerAs: 'vm',
51 controller: LoginCtrl
52};