Meteor:我如何只允许用户访问页面?
我使用的是iron router,我有一个特殊类型的帐户(Meteor.user().profile中的特定值),我想知道是否有一种使用iron router的方法,即不必在每个路由(除了home.login等)中设置限制,我只允许他们使用一页 有什么方法可以做到这一点吗?请查看此页面 基本上,您可以设置一个全局onBeforeAction钩子,该钩子将在每个路由之前运行,然后根据需要将用户重定向到默认路由。您也可以使用before进行特定的路由,下面是页面中的示例Meteor:我如何只允许用户访问页面?,meteor,Meteor,我使用的是iron router,我有一个特殊类型的帐户(Meteor.user().profile中的特定值),我想知道是否有一种使用iron router的方法,即不必在每个路由(除了home.login等)中设置限制,我只允许他们使用一页 有什么方法可以做到这一点吗?请查看此页面 基本上,您可以设置一个全局onBeforeAction钩子,该钩子将在每个路由之前运行,然后根据需要将用户重定向到默认路由。您也可以使用before进行特定的路由,下面是页面中的示例 //////////////
////////////////
// BeforeHooks
////////////////
// I use an object that contains all before hooks
var IR_BeforeHooks = {
isLoggedIn: function(pause) {
if (!(Meteor.loggingIn() || Meteor.user())) {
Notify.setError(__('Please login.'));
this.render('login');
pause();
}
},
somethingForAnyRoute: function() { ... },
...
// add more before hooks here
}
// (Global) Before hooks for any route
Router.onBeforeAction(IR_BeforeHooks.somethingForAnyRoute);
...
// Before hooks for specific routes
// Must be equal to the route names of the Iron Router route map
Router.before(IR_BeforeHooks.isLoggedIn, {only: ['userAreaA', 'userAreaB']});
此外,从Meteor.user().profile中触发路由保护可能是个坏主意,因为用户可以从客户端控制台访问和更改profile中的任何内容。为了安全起见,这应该直接放在Meteor.user()对象中,然后在服务器端查看。我不是这方面的专家,所以如果安全是一个问题,可能需要更多的研究