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