add data services and login credential management
Change-Id: I196dca93bcd68c000fe54ecd6b07046d20347321
Signed-off-by: Iftekharul Islam <iislam@us.ibm.com>
diff --git a/src/js/app.js b/src/js/app.js
index 857c9af..f19efae 100644
--- a/src/js/app.js
+++ b/src/js/app.js
@@ -10,19 +10,18 @@
$routeProvider
.when('/login', {
'templateUrl': 'login.html',
- 'controller': 'loginController'
- })
- .when('/dashboard', {
- 'templateUrl': 'dashboard.html',
- 'controller': 'dashboardController'
+ 'controller': 'loginController',
+ authenticated: false
})
.when('/power-operations', {
'templateUrl': 'power-operations.html',
- 'controller': 'powerOperationsController'
+ 'controller': 'powerOperationsController',
+ authenticated: true
})
.when('/system-overview', {
'templateUrl': 'system-overview.html',
- 'controller': 'systemOverviewController'
+ 'controller': 'systemOverviewController',
+ authenticated: true
})
.when('/unit-id', {
'templateUrl': 'unit-id.html',
@@ -38,10 +37,31 @@
'redirectTo': '/login'
});
}])
- .run(['$rootScope', '$location', 'dataService',
- function($rootScope, $location, dataService){
+ .run(['$rootScope', '$location', 'dataService', 'userModel',
+ function($rootScope, $location, dataService, userModel){
$rootScope.dataService = dataService;
dataService.path = $location.path();
+ $rootScope.$on('$routeChangeStart', function(event, next, current){
+
+ if(next.$$route == null || next.$$route == undefined) return;
+
+ if(next.$$route.authenticated){
+ if(!userModel.isLoggedIn()){
+ $location.path('/login');
+ }
+ }
+
+ if(next.$$route.originalPath == '/' ||
+ next.$$route.originalPath == '/login'){
+ if(userModel.isLoggedIn()){
+ if(current){
+ $location.path(current.$$route.originalPath);
+ }else{
+ $location.path('/system-overview');
+ }
+ }
+ }
+ });
$rootScope.$on('$locationChangeSuccess', function(event){
var path = $location.path();
dataService.path = path;
diff --git a/src/js/controllers.js b/src/js/controllers.js
index 854af78..b9490e6 100644
--- a/src/js/controllers.js
+++ b/src/js/controllers.js
@@ -1,20 +1,23 @@
angular
.module('app.controllers', [])
- .controller('loginController', ['$scope', '$window', 'APIUtils', 'dataService', function($scope, $window, APIUtils, dataService){
+ .controller('loginController', ['$scope', '$window', 'APIUtils', 'dataService', 'userModel', function($scope, $window, APIUtils, dataService, userModel){
+ $scope.dataService = dataService;
+
+ $scope.tryLogin = function(username, password, event){
+ if(event.keyCode === 13){
+ $scope.login(username, password);
+ }
+ };
$scope.login = function(username, password){
$scope.error = false;
- $scope.dataService = dataService;
if(!username || username == "" ||
!password || password == ""){
return false;
}else{
- //@TODO: service should handle
- if(username == APIUtils.LOGIN_CREDENTIALS.username &&
- password == APIUtils.LOGIN_CREDENTIALS.password){
- $window.location.hash = '#/dashboard';
+ if(userModel.login(username, password)){
+ $window.location.hash = '#/system-overview';
}else{
$scope.error = true;
- //@TODO: show error message
}
}
}
@@ -158,4 +161,4 @@
$scope.confirm = true;
$scope.immediately_confirm = true;
};
- }]);
\ No newline at end of file
+ }]);
diff --git a/src/js/directives.js b/src/js/directives.js
index af78ae7..f8aae3d 100644
--- a/src/js/directives.js
+++ b/src/js/directives.js
@@ -10,7 +10,7 @@
'scope': {
'path': '='
},
- 'controller': ['$scope','dataService', function($scope, dataService){
+ 'controller': ['$scope','dataService', 'userModel', '$location', function($scope, dataService, userModel, $location){
$scope.server_status = 01;
$scope.dataService = dataService;
APIUtils.login(function(){
@@ -25,6 +25,11 @@
});
});
+ $scope.logout = function(){
+ userModel.logout();
+ $location.path('/logout');
+ }
+
$scope.refresh = function(){
console.log("--refresh status--");
}
diff --git a/src/js/services.js b/src/js/services.js
index 38b4b85..d37dc7f 100644
--- a/src/js/services.js
+++ b/src/js/services.js
@@ -241,4 +241,28 @@
}
};
return SERVICE;
+ }])
+ .factory('userModel',['APIUtils',function(APIUtils){
+ return {
+ login : function(username, password){
+ if(username == APIUtils.LOGIN_CREDENTIALS.username &&
+ password == APIUtils.LOGIN_CREDENTIALS.password){
+ sessionStorage.setItem('LOGIN_ID', username);
+ return true;
+ }else{
+ return false;
+ }
+ },
+ isLoggedIn : function(){
+ if(sessionStorage.getItem('LOGIN_ID') === null){
+ return false;
+ }
+
+ return true;
+ },
+ logout : function(){
+ sessionStorage.removeItem('LOGIN_ID');
+ return true;
+ }
+ };
}]);
diff --git a/src/login.html b/src/login.html
index ed6f949..bad16f7 100644
--- a/src/login.html
+++ b/src/login.html
@@ -22,10 +22,10 @@
<div class="columns large-6">
<form id="login__form" role="form" action="" >
<label for="username">Username</label>
- <input type="text" id="username" name="username" required ng-model="username" ng-class="{error: error}">
+ <input type="text" id="username" name="username" required ng-model="username" ng-class="{error: error}" ng-keydown="tryLogin(username, password, $event)">
<label for="password">Password</label>
- <input type="password" id="password" name="password" class="" required ng-model="password">
+ <input type="password" id="password" name="password" class="" required ng-model="password" ng-keydown="tryLogin(username, password, $event)">
<input id="login__submit" class="btn-primary submit" type="button" value="Log in" role="button" ng-click="login(username, password)" ng-class="{error: error}">