简体   繁体   English

使用Angular和Meteor在注销时立即重定向用户

[英]Redirect user immediately on logout using Angular and Meteor

In a Meteor app that uses angular, there is a state that requires that the user is logged in. 在使用angular的Meteor应用程序中,存在一种状态,要求用户登录。

This is currently done using $meteor.requireUser() 目前,这是使用$meteor.requireUser()

function run($rootScope, $state) {
    $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {
        if(error === 'AUTH_REQUIRED') {
            $state.go('login')
        }
    })
}

and

.state('secretzone', {
    url: '/secret/zone',
    templateUrl: 'client/secret/views/zone.ng.html',
    controller: 'SecretZoneCtrl',
    resolve: {
        "currentUser": ["$meteor", function($meteor){
            return $meteor.requireUser();
        }]
    }
})

Problem: When the user is currently on the secretzone state and logs out, he should be redirected to the login state but instead still remains on the same page until he refreshes the page. 问题:当用户当前处于secretzone状态并注销时,应将其重定向到login状态,但仍保留在同一页面上,直到刷新页面为止。

How can we force the user to be redirected to the login state the moment he logs out if he is already on a page that requires him to be authenticated? 如果用户已经在需要身份验证的页面上,那么如何在用户注销时将其强制重定向到login状态?

Can this be done just by using only the UI Router, just like how it was easily achieved using Iron Router when Blaze is used? 是否可以仅使用UI路由器来完成此操作,就像使用Blaze时使用Iron路由器轻松实现的方法一样?

One way I can think of is to call $state.reload() in your logout success handler. 我能想到的一种方法是在注销成功处理程序中调用$ state.reload()。

This will, as the name suggests, reload the current state, so in this case secretzone and that will cause the resolve logic to be invoked and therefore throw the error to get picked up by your $stateChangeError handler. 顾名思义,这将重新加载当前状态,因此在这种情况下,secretzone会导致调用解析逻辑,并因此引发错误,以供您的$ stateChangeError处理程序使用。

如果您具有注销功能,为什么不直接在该功能中调用$state.go('login')

You can $watch or Meteor.Autorun the currentUser variable and add a logic inside that redirects to the login screen. 您可以$ watch或Meteor.Autorun currentUser变量并在其中添加重定向到登录屏幕的逻辑。

If you are using $rootScope.currentUser then use $watch and if you are using Meteor.currentUser use Meteor.autorun 如果使用$ rootScope.currentUser,则使用$ watch;如果使用Meteor.currentUser,则使用Meteor.autorun

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM