铁路由器路由允许/拒绝 [英] Iron Router Route Allow/Deny

查看:130
本文介绍了铁路由器路由允许/拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在写一个流星应用程序,并习惯于新的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()

这篇关于铁路由器路由允许/拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆