我现在正在写一个流星应用程序,并习惯于新的Iron Router软件包(而不是我之前使用的路由器软件包)。
I'm writing a a meteor application right now, and am getting used to the new Iron Router package (as opposed to the router package that I used before).
我有一个路由集合,应该只能由具有特定属性的用户访问。具体来说,我使用Roles包。我目前的实现方法是定义一个before函数运行条件,并重定向到登录或错误页面,如果用户没有正确的角色。这里只是一个快速(coffeescript)示例:
I have a collection of routes that should only be accessible by users with specific properties. To be specific, I'm using the Roles package. My way of achieving this at the moment is to define a before function that runs a conditional, and redirects to a login or error page if the user doesn't have the proper role. Here's just a quick (coffeescript) example:
this.route 'home', path: '/' template: 'dashboard' before: -> unless Meteor.userId() this.redirect 'userUnauthorized' do this.stop我的问题是,有更好的方法吗?看起来应该有一些方法来添加一个权限集,并将该权限集应用于路由,而不是为每个路由写入访问条件。
My question is, is there a better way to do this? It seems like there should be some way to add a permission set, and apply that permission set to a route as opposed to writing the access conditionals for every route.
推荐答案您可以在路由中设置全局之前。我不是在我目前的应用程序中使用角色,但我们是根据他们是否登录,全球重定向用户。
You could setup a global "Before" in your routes. I'm not using roles in my current app, but we are redirect users globally based on whether or not they are signed in.
您可能会这样做:
Router.before(function() { unless(Meteor.userId()) { this.redirect('userUnauthorized'); do (this.stop) } }, {except: ['userUnauthorized', 'otherPageUnauthorizedUsersAllowedToSee']});我们使用类似于 Router.configure c $ c>
We use something similar right below our Router.configure()
更多推荐
铁路由器路由允许/拒绝
发布评论