![](/img/trans.png)
[英]How to detect and logout idle user from server side in Angular 2 Meteor?
[英]Redirect user immediately on logout using Angular and Meteor
在使用angular的Meteor應用程序中,存在一種狀態,要求用戶登錄。
目前,這是使用$meteor.requireUser()
function run($rootScope, $state) {
$rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
if(error === 'AUTH_REQUIRED') {
$state.go('login')
}
})
}
和
.state('secretzone', {
url: '/secret/zone',
templateUrl: 'client/secret/views/zone.ng.html',
controller: 'SecretZoneCtrl',
resolve: {
"currentUser": ["$meteor", function($meteor){
return $meteor.requireUser();
}]
}
})
問題:當用戶當前處於secretzone
狀態並注銷時,應將其重定向到login
狀態,但仍保留在同一頁面上,直到刷新頁面為止。
如果用戶已經在需要身份驗證的頁面上,那么如何在用戶注銷時將其強制重定向到login
狀態?
是否可以僅使用UI路由器來完成此操作,就像使用Blaze時使用Iron路由器輕松實現的方法一樣?
我能想到的一種方法是在注銷成功處理程序中調用$ state.reload()。
顧名思義,這將重新加載當前狀態,因此在這種情況下,secretzone會導致調用解析邏輯,並因此引發錯誤,以供您的$ stateChangeError處理程序使用。
如果您具有注銷功能,為什么不直接在該功能中調用$state.go('login')
?
您可以$ watch或Meteor.Autorun currentUser變量並在其中添加重定向到登錄屏幕的邏輯。
如果使用$ rootScope.currentUser,則使用$ watch;如果使用Meteor.currentUser,則使用Meteor.autorun
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.