使用铁路由器重定向未登录的用户......再次 [英] Redirecting not logged-in users with iron-router... Again
问题描述
如果用户未登录(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
模板呈现在 {{产量}}
.这又不是您通常想要的(绝对不是您所期望的,因为 /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
在 /之前短暂闪烁登录
模板显示.
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.
你有没有遇到同样的问题?
Have you got this same problem?
推荐答案
好吧,看来路由上的渲染函数只是将模板渲染到当前布局中.要将模板渲染为不同的布局,您必须调用 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')
这篇关于使用铁路由器重定向未登录的用户......再次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!