Search code examples
ember.jsember-simple-auth

Ember simple auth Session not getting authenticated after calling resolve in custom authenticator


My authenticators/custom.js:

import Ember from 'ember';
import Base from 'simple-auth/authenticators/base';

export default Base.extend({
  restore: function(data) {

  },
  authenticate: function(email, password, authenticateCallback) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      Ember.$.ajax({
        type: 'POST',
        url: apiOrigin + '/api/v1/login',
        data: {
          email: email,
          password: password
        },
        dataType: 'json'
      }).then(function(userData){
        console.log('login post success', userData)
        authenticateCallback(userData)
        Ember.run(function() {
          resolve(userData.uuid)
        })
      })['catch'](function(main){
        alert('login error ' + JSON.stringify(main))
        console.error('\'caught\' error from login post request', arguments);
      })
    })
  },
  invalidate: function(data) {

  }
});

And login/controller.js:

import Ember from 'ember';

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),
  application: Ember.inject.controller(),
  actions: {
    authenticate() {
      let { identification, password } = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:custom', identification, password, (userData) => {
        //TODO set these properties on ember-simple-auth's session object instead of application controller
        this.get('application').setProperties(userData)
        this.transitionToRoute('associate-device')
      }).catch((reason) => {
        this.set('errorMessage', reason.error);
      })
    }
  }
});

My associate-device route is an AuthenticatedRoute.. I don't get an error, but instead, the last thing printed to the console is "Preparing to transition from 'login' to 'associate-device'"

Basically, ember simple auth documents here http://ember-simple-auth.com/api/classes/BaseAuthenticator.html#method_authenticate that "A resolving promise will result in the session becoming authenticated. Any data the promise resolves with will be saved in and accessible via the session service's data.authenticated property (see data). A rejecting promise indicates that authentication failed and will result in the session remaining unauthenticated." However, my session does not seem to be authenticated after I successfully resolve my promise.


Solution

  • $.ajax has no catch method. This exception was hidden because I was copy-pasting away from the documentation for writing custom authenticators. To expose any exceptions occurring in your custom authenticators authenticate method, you should probably console.log them like so:

    // app/controllers/login.js
    import Ember from 'ember';
    
    export default Ember.Controller.extend({
      session: Ember.inject.service('session'),
    
      actions: {
        authenticate() {
          let { identification, password } = this.getProperties('identification', 'password');
          this.get('session').authenticate('authenticator:oauth2', identification, password).catch((reason) => {
            // **CHANGE THE BELOW LINE**
            console.error('exception in your authenticators authenticate method', reason)
          });
        }
      }
    });