再次使用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).
关于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.
您遇到同样的问题吗?
正确答案
好,所以路线上的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
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20