【发布时间】:2015-11-02 17:02:43
【问题描述】:
我有一个与 meteor.js 同步的 onBeforeAction 方法
Router.onBeforeAction(function() {
var self;
self = this;
authToken = Session.get('authToken');
if (!authToken) {
this.redirect('login');
this.next();
} else {
Meteor.call('validateAuthToken', authToken, function (error, result)) {
if (result) {
self.next();
} else {
self.redirect('login');
self.next();
}
}
}
});
我需要通过调用服务器调用来验证存储在 Session 中的身份验证令牌。但是这个方法在我执行的时候总是会抛出异常。我发现原因是 onBeforeAction 调用在 validateAuthToken 调用返回之前终止。因此 self.next() 不会采取行动。所以我想知道我能做些什么来防止 onBeforeAction 调用停止,直到 validateAuthToken 返回验证结果然后继续?
我通过等待会话变量尝试了不同的实现,但似乎就绪状态从未设置为 true
Router.onBeforeAction(function() {
var authToken;
authToken = Session.get('authToken');
if (!authToken) {
this.redirect('login');
this.next();
} else {
Meteor.call('validateAuthToken', authToken, function (error, result) {
if (!error) {
Session.set("tokenValidated", result);
}
});
this.wait(Meteor.subscribe('token', Session.get('tokenValidated')));
if (this.ready()) {
if (!Session.get("tokenValidated")) {
this.redirect('login');
this.next();
} else {
this.next();
}
}
}
});
【问题讨论】:
标签: meteor frontend javascript iron-router