我正在尝试实现基本的登录,注销功能使用angular JS,在成功登录后,只有我的标题必须出现。一旦用户登录。在他注销之前,如果他尝试直接访问登录路由,它应该重定向到仪表板。
如何实现这一点呢?在这件事上我需要帮助。我在cookie中存储了访问令牌。这将让我们知道用户是否已登录。如果已登录..我的路由不应该通向登录路由。
作为参考,我添加了路由代码。
app.config(["$routeProvider",function($routeProvider){
$routeProvider
.when("/",{
templateUrl : 'views/home/login.html',
controller : 'homeCtrl'
})
.when("/dashboard",{
templateUrl : 'views/home/dashboard.html',
controller : 'dashboardCtrl',
authenticated : true
})
.otherwise('/',{
templateUrl : 'views/home/login.html',
controller : 'homeCtrl'
})
}]);
app.run(["$rootScope","$location","authFactory",function($rootScope,$location,authFactory){
$rootScope.$auth = authFactory;
$rootScope.$on('$routeChangeStart',function(event,next,current){
if(next.$$route.authenticated){
var userAuth = authFactory.getAccessToken();
if(!userAuth){
$location.path('/');
}
}
});
}]);如果用户当前已登录,我希望限制对登录页面的路由访问。
请帮帮我
发布于 2016-08-15 18:54:07
您可以像下面这样分别处理这两种情况。
var userAuth = authFactory.getAccessToken();
// Redirect to login if auth token is not available
if(next.$$route.authenticated && !userAuth) {
$location.path('/');
}
// Redirect to dashboard if valid auth token is available and user is in an unauthenticated page (login may be)
if(!next.$$route.authenticated && userAuth) {
$location.path('/dashboard');
}https://stackoverflow.com/questions/38953546
复制相似问题