2017-09-22 156 views
0

我想要保护像这样的路线:1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routesLoggedInUser按预期工作,但其他两种路线 - schooladminstudents不按需要工作。以管理员或学生身份登录后,根据预期,各个用户应该能够访问允许的URL,但无论何时,例如,如果学生管理员转到http://localhost/students,它会自动重定向回仪表板,并且同样为学生。我要做的是对的?谢谢。FlowRouter默认重定向到着陆页

此路线组仅允许登录的用户。

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

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

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

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

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

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