我需要在代码中添加什么来启动sessionAuthenticationFailed(错误)。现在,当我成功登录时,它可以正常工作,但我希望它在输入错误的用户名和/或密码时显示一条消息。
这是我在自定义身份验证器中进行身份验证的内容
authenticate: function(credentials) {
var _this = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.post( _this.serverTokenEndpoint, {
email: credentials.identification,
password: credentials.password
}).then(function(response) {
Ember.run(function() {
resolve({ token: response.session.token });
});
}, function(xhr, status, error) {
var response = JSON.parse(xhr.responseText);
Ember.run(function() {
reject(response.error);
});
});
});
}
我还想显示错误消息。我需要在loginController中添加什么。
答案 0 :(得分:2)
会话的authenticate
方法返回一个承诺。您可以在其中附加then
并在控制器中相应地处理它,例如:
this.get('session').authenticate('authenticator', { … }).then(function() { /*success*/ }, function() { /* error */ });
或者如果您正在使用LoginControllerMixin
:
export Ember.Route.extend(LoginControllerMixin, {
actions: {
authenticate: function() {
this._super().then(function() { /*success*/ }, function() { /* error */ });
}
}
});
无论何时验证失败,都应自动调用sessionAuthenticationFailed
,但如果您想要验证失败时显示错误消息等。我使用上述方法。