• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

再次使用Iron-Router重定向未登录的用户...

用户头像
it1352
帮助1

问题说明

如果用户未登录(Windows 7上的Meteor v0.8.0),我通常需要将用户重定向到登录页面.

I am fighting with the common need for redirecting a user to a login page if he is not logged-in (Meteor v0.8.0 on Windows 7).

关于stackoverflow也有几个类似的问题,但似乎没有答案对我有用.

There are several similar questions on stackoverflow, but no answer seems to work for me.

将无法使用#1:render()

来自文档:

onBeforeAction: function () {
  if (!Meteor.user()) {
    // render the login template but keep the url in the browser the same
    this.render('login');

    // stop the rest of the before hooks and the action function 
    this.stop();
  }
},

这里有两个问题:

1-文档已过时.不再有this.stop()函数.如此处所述,代码应为:

1- The documentation is outdated. There is no this.stop() function anymore. As stated here, the code should be :

onBeforeAction: function (pause) {
  if (!Meteor.user()) {
    // render the login template but keep the url in the browser the same
    this.render('login');

    // stop the rest of the before hooks and the action function 
    pause();
  }
},

2-仅当路由中没有layoutTemplate时,此方法才有效.如果有一个,则login模板将呈现在layoutTemplate{{>yield}}中.通常这不是您想要的登录页面.

2- This works only if the route has no layoutTemplate. If it has one, the login template is rendered in the {{>yield}} of the layoutTemplate. This is usually not what you want for a login page.

将无法工作#2:Router.go()或this.redirect()

为登录页面定义路由听起来很自然.然后,您可以执行以下操作:

Defining a route for the login page sounds like the natural way. You can then do:

Router.onBeforeAction(function(pause) {
    if (!Meteor.user()) {
        pause();
        Router.go('\login');
    }
}, {except: ['login']});

或者:

Router.onBeforeAction(function() {
    if (!Meteor.user())
        this.redirect('\login');
}, {except: ['login']});

但是奇怪的是,如果原始路由(重定向之前)具有layoutTemplate,仍然存在问题:/login模板呈现在{{yield}}内部.再次不是您通常想要的(并且绝对不是您期望的,因为/login模板没有定义layoutTemplate.)

But strangely, there is still an issue if the original route (before redirect) has a layoutTemplate: the /login template is rendered inside the {{yield}}. Which again is not what you usually want (and definitely not what you expect, as the /login template has no layoutTemplate defined).

我找到了部分解决此问题的方法:

I found a way to partially solve this:

Router.onBeforeAction(function() {
    if (!Meteor.user()) {
        var that = this;
        setTimeout(function() { that.redirect('\login'); }, 0);
    }
}, {except: ['login']});

现在一切都很好:/login模板呈现为干净的页面...除了原始路由的layoutTemplate在显示/login模板之前短暂闪烁之外.

Now everything is fine: the /login template renders as a clean page... Except that the layoutTemplate of the original route briefly blinks before the /login template is displayed.

您遇到同样的问题吗?

正确答案

#1

好,所以路线上的render函数似乎只将模板渲染到当前布局中.要将模板渲染为其他布局,您必须调用this.setLayout('templateName').一个警告似乎是您需要在登录后重新设置布局.

Ok, so it seems that the render function on a route only renders a template into the current layout. To render a template into a different layout you have to call this.setLayout('templateName'). The one caveat seems to be that you'll need to set the layout back after login.

onBeforeAction: function(pause) {
    var routeName = this.route.name;

    if (_.include(['login'], routeName))
        return;

    if (! Meteor.userId()) {
        this.setLayout("newLayout");
        this.render('login');

        //if you have named yields it the login form
        this.render('loginForm', {to:"formRegion"});

        //and finally call the pause() to prevent further actions from running
        pause();
    }else{
        this.setLayout(this.lookupLayoutTemplate());
    }
}

如果只需要登录模板,也可以通过调用this.setLayout('login')

You could also just render the login template as the layout if your login template is all you need by calling this.setLayout('login')

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcbjbeh
系列文章
更多 icon
同类精品
更多 icon
继续加载