簡體   English   中英

如果用戶已登錄,如何限制后退按鈕,或直接在angularjs中訪問登錄頁面

[英]if user is logged in, how to restrict the back button, or accessing the login page directly in angularjs

我正在嘗試使用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('/');

        }
    }
});
}]);

如果用戶當前已登錄,我想限制對登錄頁面的路由訪問。

請幫幫我

您可以如下分別處理這兩種情況。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM