blob: 586f4d0ec8548b6a04b1b6e283bf166b12a4d050 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolo035c5932016-12-14 09:55:15 -080019import {AuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo99fface2016-12-21 15:37:23 -080020import './login.scss';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080021
Matteo Scandolo828d1e82017-01-17 14:49:38 -080022import {IXosStyleConfig} from '../../../index';
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080023
24class LoginCtrl {
Matteo Scandolo9b460042017-04-14 16:24:45 -070025 static $inject = [
26 '$log',
27 'AuthService',
28 '$state',
29 'StyleConfig'
30 ];
31
Matteo Scandolo99fface2016-12-21 15:37:23 -080032 public loginStyle: any;
33 public img: string;
Matteo Scandoloebe5a222017-02-27 11:09:26 -080034 public showErrorMsg: boolean = false;
35 public errorMsg: string;
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080036 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080037 private $log: ng.ILogService,
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080038 private authService: AuthService,
Matteo Scandolob4b74a82017-01-12 13:12:26 -080039 private $state: angular.ui.IStateService,
Matteo Scandolo828d1e82017-01-17 14:49:38 -080040 private StyleConfig: IXosStyleConfig
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080041 ) {
Matteo Scandolo0e363772017-01-13 11:41:29 -080042
43 if (this.authService.isAuthenticated()) {
44 this.$state.go('xos.dashboard');
45 }
46
Matteo Scandolo828d1e82017-01-17 14:49:38 -080047 this.img = this.getImg(this.StyleConfig.background);
Matteo Scandolo99fface2016-12-21 15:37:23 -080048
49 this.loginStyle = {
Matteo Scandolo828d1e82017-01-17 14:49:38 -080050 'background-image': `url('${this.getImg(this.StyleConfig.background)}')`
Matteo Scandolo99fface2016-12-21 15:37:23 -080051 };
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080052 }
53
54 public login(username: string, password: string) {
55 this.authService.login({
56 username: username,
57 password: password
58 })
59 .then(res => {
Matteo Scandolo9b460042017-04-14 16:24:45 -070060 this.$state.go('loader');
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080061 })
Matteo Scandolo08464e52017-01-17 13:35:27 -080062 .catch(e => {
Matteo Scandolo9b460042017-04-14 16:24:45 -070063 this.$log.error(`[XosLogin] Error during login.`, e);
Matteo Scandolo1b6c06c2017-07-11 16:10:13 -070064 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 Scandoloebe5a222017-02-27 11:09:26 -080070 this.showErrorMsg = true;
Matteo Scandolo08464e52017-01-17 13:35:27 -080071 });
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080072 }
Matteo Scandolo99fface2016-12-21 15:37:23 -080073
74 private getImg(img: string) {
75 return require(`../../images/brand/${img}`);
76 }
Matteo Scandolof6acdbe2016-12-13 10:29:37 -080077}
78
79export const xosLogin: angular.IComponentOptions = {
80 template: require('./login.html'),
81 controllerAs: 'vm',
82 controller: LoginCtrl
83};