【发布时间】:2015-07-03 17:02:58
【问题描述】:
我有一个登录表单模板
<template name="loginPage">...content</template>
并且在{{currentUser}} 未登录时呈现。
问题是currentUser在页面刷新后大约一秒钟内不可用,即使用户登录也是如此。这导致登录页面出现一秒钟,即使用户已经登录,这看起来也不好.知道如何延迟呈现登录页面,以确保{{currentUser}} 是否真的登录?
[已解决]
html
{{#if loggingIn}}
please wait, user is logging in
{{/if}}
{{#unless loggingIn}}
{{#if currentUser}}
user logged in
{{/if}}
{{#unless currentUser}}
login form
{{/unless}}
{{/unless}}
js
// create global {{#if loggingIn}}{{/if}} helper
Session.set("loggingIn", true);
var loginWait = 3; // seconds
var loginTimeout = setInterval(function(){
loginWait = loginWait - 1;
if(loginWait <= 0 || Meteor.user()){
Session.set("loggingIn", false);
clearInterval(loginTimeout);
}
},1000);
Handlebars.registerHelper('loggingIn', function () {
return Session.get("loggingIn");
});
【问题讨论】:
-
使用路由来实现这一点,以及路由器挂钩 (
onBeforeAction)。所以路由器会检查用户是否登录,如果没有登录,你执行this.redirect()去另一个路由,或者this.render()渲染一个模板。见:iron-meteor.github.io/iron-router/#using-redirects
标签: javascript meteor meteor-blaze meteor-accounts