2016-08-15 3 views
0

私は基本的なログインを実装しようとしています。 ユーザーがログアウトするまでユーザーがログインすると、ログインルートに直接アクセスしようとすると、ダッシュボードにリダイレクトされます。もしあなたがログインしている場合、戻るボタンを制限する方法、またはangularjsのログインページに直接アクセスする方法

これを実現する方法は?私はこの問題で助けが必要です。 私はクッキーに格納されているアクセストークンを持っています。ユーザーがログインしているかどうかを知らせるメッセージが表示されます。 ログインしている場合..私のルートはログインルートになってはなりません。

参考のために私はルートのコードを追加しています。私は、ユーザーが現在..

にログインしている場合はログインページにルートアクセスを制限したい

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

ありがとうございました。 –

関連する問題