0

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.

user3813559
  • 123
  • 3
  • 14

1 Answers1

2

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.

marcoow
  • 4,062
  • 1
  • 14
  • 21
  • Just a little clarification. When I run resolve({ token: response.session.token }) and it does not find a session.token is that automatically considered a failed. Is that the promise? I'm just wondering if an error in my debugger is suppose to show up if there is a failed login attempt. – user3813559 Aug 25 '14 at 14:27
  • 1
    When you resolve from the authenticator with whatever the session is considered authenticated, when you reject authentication is considered to have failed. – marcoow Aug 25 '14 at 14:29
  • For the success part of the function where do I store the data so it can be accessed by restore when a user refreshes the page? – user3813559 Aug 25 '14 at 23:37
  • 1
    You don't store that data. The authenticator resolves with the data which causes the session to automatically store it. You're already doing that above: `Ember.run(function() { resolve({ token: response.session.token }); });` – marcoow Aug 26 '14 at 07:10