0

我需要在代码中添加什么来启动 sessionAuthenticationFailed(error)。现在,当我成功登录时它可以工作,但我希望它在输入错误的用户名和/或密码时显示一条消息。

这是我在自定义身份验证器中进行身份验证的内容

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);
                });
            });
        });
    }

我还想显示一条错误消息。我需要在我的登录控制器中放入什么。

4

1 回答 1

2

会话的authenticate方法返回一个承诺。您可以附加 athen并在您的控制器中相应地处理它,例如:

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都应该自动调用,但是如果您想在身份验证失败时显示错误消息等。我会使用上述方法。

于 2014-08-25T09:21:00.127 回答