再次使用Iron-Router重定向未登录的用户...
如果用户未登录(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).
关于*也有几个类似的问题,但似乎没有答案对我有用.
There are several similar questions on *, 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.
您遇到同样的问题吗?
好,所以路线上的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')