简体   繁体   English

ember-cli自定义身份验证器简单身份验证会话身份验证失败

[英]ember-cli custom authenticator simple auth session authentication failed

What do I need to add to the code to initiate the sessionAuthenticationFailed(error). 我需要添加什么代码来启动sessionAuthenticationFailed(error)。 Right now it works when I have a successful login but I would like it also to show a message when when an incorrect username and/or password is entered. 现在,当我成功登录后它可以工作,但是当用户名和/或密码输入错误时,我也希望它显示一条消息。

here is what I have within authenticate in my custom authenticator 这是我自定义身份验证器中身份验证的内容

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

I would also like to show an error message. 我还想显示一条错误消息。 What do I need to put in my loginController. 我需要在我的loginController中放入什么。

The session's authenticate method returns a promise. 会话的authenticate方法返回一个Promise。 You can attach a then to that and handle it accordingly in your controller, eg: 您可以在其上附加一个thenthen在您的控制器中对其进行相应处理,例如:

this.get('session').authenticate('authenticator', { … }).then(function() { /*success*/ }, function() { /* error */ });

or if you're using the LoginControllerMixin : 或者,如果您使用的是LoginControllerMixin

export Ember.Route.extend(LoginControllerMixin, {
  actions: {
    authenticate: function() {
      this._super().then(function() { /*success*/ }, function() { /* error */ });
    }
  }
});

The sessionAuthenticationFailed should be called automatically anyway whenever authentication fails but if you want to eg display an error message when authentication fails etc. I'd use above approach. 无论何时只要身份验证失败, sessionAuthenticationFailed应该自动调用sessionAuthenticationFailed但是如果您想例如在身份验证失败时显示错误消息等。我将使用上述方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM