我正在尝试这样的受保护路线:1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routes
。 LoggedInUser
按预期工作,但其他两条路线 - schooladmin
和students
无法正常工作。
以管理员或学生身份登录后,根据期望,各个用户应该能够转到允许的网址,但是,例如,每当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'});
}
});
答案 0 :(得分:0)
Roles包实际上取决于订阅,这意味着如果订阅尚未就绪,Roles.userIsInRole
方法将始终返回false
。然后你的路线失败了,因为无论如何,FlowRouter总是运行。这种情况发生在非常具体的情况下,但它会发生,您的用户会注意到。
幸运的是现在有一个修复。我们可以完全控制FlowRouter何时初始化。
有两种方法可以实现这一目标。
在FlowRouter声明上方使用Accounts.onLogin(function(user){});
方法检查角色,然后重定向。 (检查角色的user._id)
点击此处查看第二个解决方案https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42