【问题标题】:FlowRouter defaulted redirect to the landing pageFlowRouter 默认重定向到登陆页面
【发布时间】:2018-03-04 13:07:45
【问题描述】:

我正在尝试使用这样的受保护路由:1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routesLoggedInUser 按预期工作,但其他 2 条路线 - schooladminstudents 无法按需要工作。

在以管理员或学生身份登录后,根据预期,各个用户应该能够访问允许的 url,但例如,如果 schooladmin 管理员访问http://localhost/students,它会自动重定向回来到仪表板,同样适用于学生。我该怎么做才对?

此路由组只允许登录用户。

var LoggedInUser = FlowRouter.group({
  name: 'currentUser', triggersEnter: [function (context, redirect) {
    if (Meteor.loggingIn() || Meteor.userId()) {
      FlowRouter.watchPathChange();
      let currentRoute = FlowRouter.current();
      if (!currentRoute.path) {
        FlowRouter.go('/dashboard');
      } else {
        FlowRouter.go(currentRoute.path);
      }

    } else {
      redirect('/');
    }
  }]
});

这是学校管理员的路由组

var schooladmin = LoggedInUser.group({
  name: 'schooladmins', triggersEnter: [function (context, redirect) {
    FlowRouter.watchPathChange();
    let currentRoute = FlowRouter.current();
    if (Roles.userIsInRole(Meteor.userId(), ['super-admin', 'admin'])) {
      console.log(currentRoute.path);
      FlowRouter.go(currentRoute.path);
    } else {
      redirect('dashboard');
    }
  }]
});

这是学生的路线

var students = LoggedInUser.group({
  name: 'students', triggersEnter:[function (context, redirect) {
    FlowRouter.watchPathChange();
    let currentRoute = FlowRouter.current();
    if (Roles.userIsInRole(Meteor.userId(), ['manage-team', 'student-page'])) {
      FlowRouter.go(currentRoute.path);
    } else {
      redirect('dashboard');
    }
  }]
});

组附加到的示例路由 此示例路线仅供学校管理员访问

schooladmin.route('/students', {
  name: 'students', action(){
    BlazeLayout.render('formrender', {formrend: 'student'});
  }
});

此路线供学生使用

students.route('/student/dashboard', {
  name: 'students-dashboard', action(){
    BlazeLayout.render('studentlayout', {studentrender: 'studentdashboard'});
  }
});

【问题讨论】:

    标签: meteor url-routing url-redirection meteor-blaze flow-router


    【解决方案1】:

    角色包实际上依赖于订阅,这意味着如果订阅未准备好,Roles.userIsInRole 方法将始终返回 false。然后你的路由失败了,因为 FlowRouter 无论如何都会运行。这发生在非常特殊的情况下,但它会发生并且您的用户会注意到。

    幸运的是,现在有一个修复程序。我们可以完全控制 FlowRouter 何时初始化。

    有两种方法可以实现这一点。

    1. 就在 FlowRouter 声明上面使用Accounts.onLogin(function(user){}); 方法检查角色然后重定向。 (检查 user._id 的角色)

    2. 点击此处查看第二个解决方案https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 2011-07-30
      • 2017-07-18
      • 1970-01-01
      相关资源
      最近更新 更多