[英]FlowRouter defaulted redirect to the landing page
我正在嘗試采用以下保護路線: 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
。 LoggedInUser
可以按預期工作,但其他2條路徑schooladmin
和students
無法按需工作。
以管理員或學生身份登錄后,根據期望,相應的用戶應該能夠訪問允許的網址,但是例如,如果學校管理員管理員訪問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'});
}
});
Roles包實際上取決於訂閱,這意味着如果訂閱尚未准備好,那么Roles.userIsInRole
方法將始終返回false
。 然后,您的路由就會失敗,因為無論如何,FlowRouter始終都會運行。 這在非常特定的情況下會發生,但是會發生,您的用戶會注意到。
幸運的是,現在有了一個修復程序。 我們可以完全控制FlowRouter何時初始化。
有兩種方法可以實現此目的。
在FlowRouter聲明的正上方,使用Accounts.onLogin(function(user){});
檢查角色然后重定向的方法。 (檢查user._id以獲取角色)
單擊此處獲取第二個解決方案https://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.