Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 1 | import {AuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 2 | import './login.scss'; |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 3 | import {IXosModelSetupService} from '../services/helpers/model-setup.helpers'; |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame^] | 4 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 5 | |
| 6 | class LoginCtrl { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame^] | 7 | static $inject = ['AuthService', '$state', 'ModelSetup', 'StyleConfig']; |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 8 | public loginStyle: any; |
| 9 | public img: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 10 | /** @ngInject */ |
| 11 | constructor( |
| 12 | private authService: AuthService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 13 | private $state: angular.ui.IStateService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame^] | 14 | private ModelSetup: IXosModelSetupService, |
| 15 | private StyleConfig: IXosStyleConfig |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 16 | ) { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 17 | |
| 18 | if (this.authService.isAuthenticated()) { |
| 19 | this.$state.go('xos.dashboard'); |
| 20 | } |
| 21 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame^] | 22 | this.img = this.getImg(this.StyleConfig.background); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 23 | |
| 24 | this.loginStyle = { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame^] | 25 | 'background-image': `url('${this.getImg(this.StyleConfig.background)}')` |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 26 | }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | public login(username: string, password: string) { |
| 30 | this.authService.login({ |
| 31 | username: username, |
| 32 | password: password |
| 33 | }) |
| 34 | .then(res => { |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 35 | // after login set up models |
| 36 | return this.ModelSetup.setup(); |
| 37 | }) |
| 38 | .then(() => { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 39 | this.$state.go('xos.dashboard'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 40 | }) |
| 41 | .catch(e => console.error); |
| 42 | } |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 43 | |
| 44 | private getImg(img: string) { |
| 45 | return require(`../../images/brand/${img}`); |
| 46 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | export const xosLogin: angular.IComponentOptions = { |
| 50 | template: require('./login.html'), |
| 51 | controllerAs: 'vm', |
| 52 | controller: LoginCtrl |
| 53 | }; |