当前位置: 代码迷 >> JavaScript >> 流星铁路由器重定向,当它不应该
  详细解决方案

流星铁路由器重定向,当它不应该

热度:104   发布时间:2023-06-13 12:34:57.0

我有这个onBeforeAction挂钩,在该挂钩中,我检查用户是否未以特定角色(在这种情况下为雇主角色)登录或未登录,然后将其重定向到特定页面(称为验证)。 我可以很好地导航到该页面,但是当我已经在页面上并单击REFRESH时,似乎他通过了IF条件并继续使用Router.go("verification");

编码:

Router.route("/employer/profile", {
  name:"employerProfile",
  template:"employerProfile",
  layoutTemplate:'employerLayout',

    onBeforeAction:function(){

        var user = Meteor.userId();
        if(!user || !Roles.userIsInRole(user, ['employer'])) { // checking if the user satisfies this condition e.g. if its a "GUEST USER" than he can't access this route
              Router.go("verification");
        }
        else {
          this.next();
        }
        return true;

    },


});

这是一个快速的视频演示,演示发生了什么:

有人有主意吗?

看起来在页面刷新上,Meteor订阅了Roles集合,但尚未下载任何数据,因此userIsInRole返回false。

我认为您要么需要等到角色订阅准备就绪(这不是最佳选择),要么稍后再等到订阅准备就绪时重定向到验证页面。 我建议使用服务器方法进行此检查。 将验证代码移动到服务器方法中,以供使用:Meteor.call('verifyUser',函数(错误,结果){if(result === false)Router.go(“ verification”);});

  相关解决方案