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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 3 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 4 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 5 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 6 | |
| 7 | class LoginCtrl { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 8 | static $inject = ['$log', 'AuthService', '$state', 'XosModelDiscoverer', 'StyleConfig']; |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 9 | public loginStyle: any; |
| 10 | public img: string; |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 11 | public showErrorMsg: boolean = false; |
| 12 | public errorMsg: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 13 | constructor( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 14 | private $log: ng.ILogService, |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 15 | private authService: AuthService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 16 | private $state: angular.ui.IStateService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 17 | private XosModelDiscoverer: IXosModelDiscovererService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 18 | private StyleConfig: IXosStyleConfig |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 19 | ) { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 20 | |
| 21 | if (this.authService.isAuthenticated()) { |
| 22 | this.$state.go('xos.dashboard'); |
| 23 | } |
| 24 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 25 | this.img = this.getImg(this.StyleConfig.background); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 26 | |
| 27 | this.loginStyle = { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 28 | 'background-image': `url('${this.getImg(this.StyleConfig.background)}')` |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 29 | }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | public login(username: string, password: string) { |
| 33 | this.authService.login({ |
| 34 | username: username, |
| 35 | password: password |
| 36 | }) |
| 37 | .then(res => { |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 38 | this.showErrorMsg = false; |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 39 | // after login set up models |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 40 | return this.XosModelDiscoverer.discover(); |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 41 | }) |
| 42 | .then(() => { |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 43 | this.$state.go('xos.dashboard'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 44 | }) |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 45 | .catch(e => { |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 46 | this.$log.error(`[XosLogin] Error during login.`); |
| 47 | this.errorMsg = `Something went wrong, please try again.`; |
| 48 | this.showErrorMsg = true; |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 49 | }); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 50 | } |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 51 | |
| 52 | private getImg(img: string) { |
| 53 | return require(`../../images/brand/${img}`); |
| 54 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | export const xosLogin: angular.IComponentOptions = { |
| 58 | template: require('./login.html'), |
| 59 | controllerAs: 'vm', |
| 60 | controller: LoginCtrl |
| 61 | }; |