0

我的基本设置是我有一个登录页面、代表页面和经理页面。

我已经在 Iron 路由器中设置了我的路由,并在之前添加了一些钩子以验证是否存在具有适当角色的用户。对于登录页面,我想查看是否有用户,什么类型的用户(使用角色),如果他们已经登录,然后发送到他们的正确页面。对于用户类型页面,我想检查是否有用户,确保他们在正确的位置,如果没有,将他们发送回登录页面或正确的页面。

每个用户在 profile.organization 中都有一个角色('rep',organizationId)和一个组织。我检查他们在组织中的角色。

这一切都很好,除非有人恢复会话。如果他们从注销开始,它会顺利进行,但如果他们昨天登录并打开网站备份,它只是位于登录页面。我认为这是因为他们正在重新登录。

所以,问题是,处理检查用户是否登录或登录的最佳方法是什么。棘手的部分是我需要从用户配置文件中获取数据以确定将它们发送到哪里,所以如果他们正在登录,我觉得我做不到。我怀疑它与 this.redirect('login') 与 this.render('login') 有关

这是我的登录路线代码

loginsignupController = BaseController.extend ({
    layoutTemplate: 'loginLayout',
    loadingTemplate: 'loading',
    yieldTemplates: {      
      'header': {to:'header'}
    },
    before: function() {      
      //check if logged in
      if(!!Meteor.user()){
        var org = Meteor.user().profile.organization;
        console.log('logged in');
        //check for rep, manager
        if (Roles.userIsInRole(Meteor.userId(), 'rep', org) ||   Roles.userIsInRole(Meteor.userId(), 'rep', 'default')){
          this.redirect('today')
        } else if (Roles.userIsInRole(Meteor.userId(), ['manager'], org)) {
          //if manager, send to manager home
          this.redirect('managerHome')
        }
      }
    }   
});

这是代表路线(用户类型a)

RepController = BaseController.extend({
  layoutTemplate: 'repLayout',
  loadingTemplate: 'loading',
  yieldTemplates: {
    'sidebar': {to: 'sidebar'},
    'header': {to:'header'}
  },
  waitOn: function() {
  },
  data: function () {
  },
  before: function(){
      //check if logged in
      if(!Meteor.loggingIn() && !Meteor.user()) {
          this.redirect("/login");
      } else {
        var org = Meteor.user().profile.organization;
        console.log('logged in');
        //check for rep, manager
        if (Roles.userIsInRole(Meteor.userId(), ['manager'], org)) {        
          //if manager, send to manager home          
          this.redirect('/managerHome')          
        }
      };
   }})

谢谢!

4

1 回答 1

1

发生这种行为是因为您尚未处理用户数据尚未从服务器到达的情况。

它在用户首先注销时起作用,因为他们必须完成所有订阅才能登录。

所以当你直接加载浏览器页面时会发生什么,你试图读取用户,但用户还没有到达,它有点卡住了。before不是被动的,所以当用户登录时,什么都不会改变。

要解决此问题,您需要发布用户的数据并在您的之前waitOn或之前订阅它this.subscribe(..).wait()

要记住的另一件事是Meteor.user()在初始加载期间更改 3 次:

  • 第一次null直到一些数据从服务器到达
  • 然后一些数据到达,但配置文件字段为空(不确定为什么会这样)。你直接调用Meteor.user().profile.organizationwhen Meteor.user().profilewill be null,会报错
  • 最后,所有数据都将被填充并准备好访问。
于 2014-02-21T20:08:51.960 回答