我是angularjs中的新手,并尝试在angularjs中创建一个登录应用程序,其中flow应如下所示:
1.当用户输入userneme
和password
并按Enter按钮时,应将其重定向到dashboard page
。
如果用户按下浏览器的后退按钮,则不应再次进入登录页面。仅当redirected to login page
被按下时才应logout button
。
到store
credentials in cookies
,以便{}} hitting same url
中的用户为different tab
时,应通过检查dashbord
直接打开stored cookies
}。
我的js code
如下first two condition
我上面提到的内容已经实现。但third one
我无法到现在为止。
var app=angular.module('myApp', ['ngRoute','ngCookies']);
console.log("in appnew.js")
app.config(function($routeProvider,$locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/',{
templateUrl: 'login_admin.html',
controller: 'userController'
})
.when('/dashboard',{
templateUrl: 'dashboard.html',
controller: 'userController',
authenticated: true
})
.when('/logout',{
templateUrl: 'logout_admin.html',
controller: 'userController',
authenticated: true
})
.otherwise({
redirectTo: "/"
});
});
app.factory('userModel', function($http,$cookies,$location){
var userModel={};
userModel.login= function(loginfrm){
data={
jeeb_no: loginfrm.jeeb_no,
//password: loginfrm.jeeb_no
};
console.log("loginfrm"+loginfrm);
return $http.post("http://-----------:8080/admin_login", data).
success(function(response){
$cookies.put('auth',response);
console.log('response'+response);
}).
error(function(response){
console.log("response"+response);
});
};
userModel.getAuthStatus=function(){
var status = $cookies.get('auth');
if(status){
return true;
}
else{
return false;
}
};
userModel.doUserLogout = function(){
$cookies.remove('auth');
}
return userModel;
});
app.run(["$rootScope",'$location','userModel', function($rootScope,$location,userModel){
$rootScope.$on('$routeChangeStart', function(event, next, current){
console.log("event "+event);
console.log("next "+next);
console.log("current "+current);
if (next.$$route.authenticated) {
console.log("next.$$route.authenticated"+next.$$route.authenticated);
if (!userModel.getAuthStatus()) {
console.log("getAuthStatus"+!userModel.getAuthStatus);
$location.path('/');
}
}
if (next.$$route.originalPath =='/') {
console.log("next.$$route.originalPath "+next.$$route.originalPath);
/*console.log("current.$$route.originalPath "+$route.current.$$route.originalPath);*/
if (userModel.getAuthStatus()) {
console.log("current "+current);
console.log("next "+next);
console.log("current.$$route.originalPath"+current.$$route.originalPath);
$location.path(current.$$route.originalPath);
}
}
/*if(current.$$route.originalPath == '/dashboard' && current.$$route.originalPath == '/logout'){
}*/
});
}]);
app.controller('userController',function($scope,$location,userModel){
angular.extend($scope,{
login: function(adminfrm){
var data={
jeeb_no: $scope.admin.name,
password: $scope.admin.password
};
userModel.login(data).then(function(){
$location.path('/dashboard');
});
},
logout: function(){
userModel.doUserLogout();
$location.path('/');
}
});
});
app.factory('userModel', function($http,$cookies,$location){
var userModel={};
userModel.login= function(loginfrm){
data={
jeeb_no: loginfrm.jeeb_no,
//password: loginfrm.jeeb_no
};
console.log("loginfrm"+loginfrm);
return $http.post("http://---------:8080/admin_login", data).
success(function(response){
$cookies.put('auth',response);
console.log('response'+auth);
}).
error(function(response){
console.log("response"+response);
});
};
userModel.getAuthStatus=function(){
var status = $cookies.get('auth');
if(status){
return true;
}
else{
return false;
}
};
userModel.doUserLogout = function(){
$cookies.remove('auth');
}
return userModel;
})