user3813559
user3813559

Reputation: 123

ember-cli custom authenticator simple auth session authentication failed

What do I need to add to the code to initiate the 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.

Upvotes: 0

Views: 405

Answers (1)

marcoow
marcoow

Reputation: 4062

The session's authenticate method returns a promise. You can attach a then to that and handle it accordingly in your controller, e.g.:

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

or if you're using the 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 e.g. display an error message when authentication fails etc. I'd use above approach.

Upvotes: 2

Related Questions