2016-08-15 37 views
0

我想实现基本登录,使用角JS注销功能,成功登录然后只有我的头必须出现。 一旦用户登录,直到他注销,如果他尝试直接访问登录路由,它应该重定向到仪表板。如果用户登录,如何限制后退按钮或直接访问登录页面angularjs

如何实现这个..?我在这件事上需要帮助。 我有访问令牌存储在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('/'); 

     } 
    } 
}); 
}]); 

我想限制路线进入登录页面,如果用户在当前登录..

请帮我

回答

1

可以分别处理两种情况如下图所示。

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'); 
} 
+0

嘿,非常感谢..它的工作... –

相关问题