2016-08-15 38 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

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

相關問題