简体   繁体   English

FlowRouter默认重定向到登录页面

[英]FlowRouter defaulted redirect to the landing page

I am trying to have protected routes like these: 1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routes 我正在尝试采用以下保护路线: 1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routes 1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routes . 1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routes The LoggedInUser works as expected but the 2 other routes - schooladmin and students does not work as needed. LoggedInUser可以按预期工作,但其他2条路径schooladminstudents无法按需工作。

After logging in as an admin or as a student, according to the expectation the respective users should be able to go to the allowed urls but whenever, as an example, if a schooladmin admin goes to http://localhost/students it automatically redirects back to dashboard, and likewise for student. 以管理员或学生身份登录后,根据期望,相应的用户应该能够访问允许的网址,但是例如,如果学校管理员管理员访问http:// localhost / students,它将自动重定向回到仪表板,同样适用于学生。 What am I to do right? 我该怎么办?

This route group allows only logged in users. 该路由组仅允许已登录的用户。

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('/');
    }
  }]
});

This is the route group for school admins 这是学校管理员的路线组

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');
    }
  }]
});

This is the route for students 这是学生的路线

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');
    }
  }]
});

Sample routes the groups are attached to This sample route is for school admins only to access 组附加到的示例路线此示例路线仅供学校管理员访问

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

this route is for student to access 这条路线供学生使用

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

The Roles package actually depends on a subscription, which means that if the subscription is not ready the Roles.userIsInRole method will always return false . Roles包实际上取决于订阅,这意味着如果订阅尚未准备好,那么Roles.userIsInRole方法将始终返回false And then your route fails, because FlowRouter always runs no matter what. 然后,您的路由就会失败,因为无论如何,FlowRouter始终都会运行。 This happens in very specific cases, but it happens and your users will notice. 这在非常特定的情况下会发生,但是会发生,您的用户会注意到。

Fortunately there is now a fix. 幸运的是,现在有了一个修复程序。 We can have full control over when FlowRouter initializes. 我们可以完全控制FlowRouter何时初始化。

There are 2 ways to achieve this. 有两种方法可以实现此目的。

  1. Just above FlowRouter Declaration use Accounts.onLogin(function(user){}); 在FlowRouter声明的正上方,使用Accounts.onLogin(function(user){}); method to check role and then redirect. 检查角色然后重定向的方法。 (check user._id for Roles) (检查user._id以获取角色)

  2. Click here for second solution https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42 单击此处获取第二个解决方案https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42

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

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