Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 19 | import {AuthService} from '../../datasources/rest/auth.rest'; |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 20 | import './login.scss'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 21 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 22 | import {IXosStyleConfig} from '../../../index'; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 23 | |
| 24 | class LoginCtrl { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 25 | static $inject = [ |
| 26 | '$log', |
| 27 | 'AuthService', |
| 28 | '$state', |
| 29 | 'StyleConfig' |
| 30 | ]; |
| 31 | |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 32 | public loginStyle: any; |
| 33 | public img: string; |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 34 | public showErrorMsg: boolean = false; |
| 35 | public errorMsg: string; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 36 | constructor( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 37 | private $log: ng.ILogService, |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 38 | private authService: AuthService, |
Matteo Scandolo | b4b74a8 | 2017-01-12 13:12:26 -0800 | [diff] [blame] | 39 | private $state: angular.ui.IStateService, |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 40 | private StyleConfig: IXosStyleConfig |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 41 | ) { |
Matteo Scandolo | 0e36377 | 2017-01-13 11:41:29 -0800 | [diff] [blame] | 42 | |
| 43 | if (this.authService.isAuthenticated()) { |
| 44 | this.$state.go('xos.dashboard'); |
| 45 | } |
| 46 | |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 47 | this.img = this.getImg(this.StyleConfig.background); |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 48 | |
| 49 | this.loginStyle = { |
Matteo Scandolo | 828d1e8 | 2017-01-17 14:49:38 -0800 | [diff] [blame] | 50 | 'background-image': `url('${this.getImg(this.StyleConfig.background)}')` |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 51 | }; |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public login(username: string, password: string) { |
| 55 | this.authService.login({ |
| 56 | username: username, |
| 57 | password: password |
| 58 | }) |
| 59 | .then(res => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 60 | this.$state.go('loader'); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 61 | }) |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 62 | .catch(e => { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 63 | this.$log.error(`[XosLogin] Error during login.`, e); |
Matteo Scandolo | 1b6c06c | 2017-07-11 16:10:13 -0700 | [diff] [blame] | 64 | if (e.error === 'XOSNotAuthenticated') { |
| 65 | this.errorMsg = `This combination of username/password cannot be authenticated`; |
| 66 | } |
| 67 | else { |
| 68 | this.errorMsg = `Something went wrong, please try again.`; |
| 69 | } |
Matteo Scandolo | ebe5a22 | 2017-02-27 11:09:26 -0800 | [diff] [blame] | 70 | this.showErrorMsg = true; |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 71 | }); |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 72 | } |
Matteo Scandolo | 99fface | 2016-12-21 15:37:23 -0800 | [diff] [blame] | 73 | |
| 74 | private getImg(img: string) { |
| 75 | return require(`../../images/brand/${img}`); |
| 76 | } |
Matteo Scandolo | f6acdbe | 2016-12-13 10:29:37 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | export const xosLogin: angular.IComponentOptions = { |
| 80 | template: require('./login.html'), |
| 81 | controllerAs: 'vm', |
| 82 | controller: LoginCtrl |
| 83 | }; |