0

私は1). loggedin user group routes 2.) admin routes, 3.) student routes, 4.) public routesのような経路を保護しようとしています。 LoggedInUserは期待通りに機能しますが、他の2つのルート - 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 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'); 
    } 
    }] 
}); 

サンプルルートのルートである学校の管理者のためのルートグループ

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

ですアクセスするには

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の初期化を完全に制御できます。

これを達成するには2つの方法があります。

  1. FlowRouter宣言のすぐ上に、ロールをチェックしてからリダイレクトする方法を使用します。 (ロールのuser._idチェック)

  2. 第二の溶液はこちらをクリックしてhttps://medium.com/@satyavh/using-flow-router-for-authentication-ba7bb2644f42

関連する問題