简体   繁体   English

在流星应用程序上使用Iron-Router和onBeforeAction进行重定向

[英]Redirection with iron-router and onBeforeAction on meteor app

I have a meteor app and with the package iron-router , I try to block all pages if the user is not connected except fews pages. 我有一个流星应用程序,并且带有包iron-router ,如果用户未连接,则尝试阻止所有页面,只有很少的页面。 And if nothing is specified we go on the landing page. 如果未指定任何内容,我们将进入登录页面。 So in the file router.js, I have : 所以在文件router.js中,我有:

Router.onBeforeAction(function() {
    if (!Meteor.userId()) { 
        Router.go('login');
    } else {
    this.next();
    }
}, {
except: [
    "login", "landing", "register", "forgotPassword" 
]
});

Router.route('/', function () {
    Router.go('landing');
});

But when I go on localhost:3000/ I'm redirected to login page and not to the landing page. 但是当我进入localhost:3000/我被重定向到登录页面,而不是登录页面。

If I remove the onBeforeAction function, I'm redirect to the landing page. 如果删除onBeforeAction函数,则会重定向到登录页面。 So it must be a problem with this 2 functions but I don't know where. 因此这2个功能一定有问题,但我不知道在哪里。 Maybe I need to precise the "/" in the exceptions but it doesn't work. 也许我需要在例外中对“ /”进行精确调整,但它不起作用。 Do you have an idea ? 你有想法吗 ?

You need to define the route '/' in your exceptions too, otherwise this is caught by the onBeforeAction 您还需要在异常中定义路由'/' ,否则将被onBeforeAction

Try re-defining as follows 尝试如下重新定义

Router.onBeforeAction(function() {
    if (!Meteor.userId()) { 
        Router.go('login');
    } else {
    this.next();
    }
}, {
except: [
    "default", "login", "landing", "register", "forgotPassword" 
]
});

Router.route('/', function () {
    Router.go('landing');
}, { 
    name: "default"
} );

In this case you name the route and then you can add it to your exception list 在这种情况下,您可以为路线命名,然后可以将其添加到例外列表中

see http://iron-meteor.github.io/iron-router/#named-routes 参见http://iron-meteor.github.io/iron-router/#named-routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM