0
我使用Devise在我的Rails和Angular應用程序中進行身份驗證。我試圖根據用戶是否通過身份驗證來對其中一個狀態執行條件語句。AngularJS:基於驗證的UI路由器條件
我使用onEnter回調函數來確定用戶是否通過身份驗證。
路線:
// Dashboard state
.state('dashboard', {
url: '/dashboard',
templateUrl: 'dashboard.html',
controller: 'MainCtrl',
})
// Login state
.state('login', {
url: '/login',
templateUrl: '_login.html',
controller: 'AuthCtrl',
onEnter: ['$state', 'Auth', function($state, Auth) {
Auth.currentUser().then(function(){
$state.go('dashboard');
})
}]
})
// Register state
.state('register', {
url: '/register',
templateUrl: '_register.html',
controller: 'AuthCtrl',
onEnter: ['$state', 'Auth', function($state, Auth) {
Auth.currentUser().then(function(){
$state.go('dashboard');
})
}]
})
$urlRouterProvider.otherwise('dashboard');
導航控制器
// NAV controller
// ------------------------------
.controller('NavCtrl', ['$scope', 'Auth',
// Main scope (used in views)
function($scope, Auth) {
$scope.signedIn = Auth.isAuthenticated;
$scope.logout = Auth.logout;
Auth.currentUser().then(function (user){
$scope.user = user;
});
$scope.$on('devise:new-registration', function (e, user){
$scope.user = user;
});
$scope.$on('devise:login', function (e, user){
$scope.user = user;
});
$scope.$on('devise:logout', function (e, user){
$scope.user = {};
});
}
])
身份認證控制器
// Authentification controller
// ------------------------------
.controller('AuthCtrl', ['$scope', '$state', 'Auth',
// Main scope (used in views)
function($scope, $state, Auth) {
$scope.login = function() {
Auth.login($scope.user).then(function(){
$state.go('home');
});
};
$scope.register = function() {
Auth.register($scope.user).then(function(){
$state.go('home');
});
};
}
]);
如何確定儀表盤上的狀態未驗證用戶,並重定向他們登錄?