Search code examples
ember.jspromiseember-simple-auth

Ember RSVP Promise resolve always returns undefined value


I'm using Ember-simple-auth and trying to return data from my custom authenticator back into the controller that did the authenticating.

in my authenticator/custom.js:

  authenticate(identification, password) {
      return  new Ember.RSVP.Promise(function (resolve, reject) {
          var loginURL = 'https://domain.com/login';    
          var authObj = Ember.Object.create({"identity":identification,"password":password});
          var hash = authObj.getProperties('identity', 'password');
          var stringHash = JSON.stringify(hash);

              var xhr = new XMLHttpRequest();
              xhr.open("post", loginURL);
              xhr.onreadystatechange = handler;
              xhr.responseType = 'json';
              xhr.setRequestHeader('Content-Type', 'application/json');
              xhr.setRequestHeader('Format', 'JSON');
              xhr.send(stringHash);

              function handler() {
                if (this.readyState === this.DONE) {
                  if (this.status === 200) {
                    console.log('response is: ',this.response); /// <-- returns good response data
                    resolve(this.response);
                  } else {
                      reject( 'failed with status: [' + this.status + ']');

                  }
                }
            }

and in my login controller:

 authenticate() {
  let { identification, password } = this.getProperties('identification', 'password');

  var session = this.get('session');
  session.authenticate('authenticator:custom', identification, password).then((reason) => {
            console.log('failed login',reason);
        });   
    }
},

But I'd really like to be able to handle the resolve function and get it's value payload from the authenticate promise.

If I change the .catch to a .then the response function is successfully called but always has an undefined value as its payload:

  session.authenticate('authenticator:custom', identification, password).then(
      (response) => {
          console.log('response: ',response); //// <---- always 'undefined'
            this.setUserWithData(response);
        },
      (reason) => {
            this.set('Login failed: ',reason);
        }       

    );    
    }

Even if I restructure the promise, rearrange how the function is called, the first function from an RSVP is successfully called, but has an undefined payload. The second function from an RSVP always has a correct payload.

I tried reversing the resolve/reject:

Ember.RSVP.Promise(function (resolve, reject){

to

Ember.RSVP.Promise(function (reject, resolve){

and the function successfully carries the response, but the simple-auth now believes it has failed its authorization.

I'd like to be able to pass the response payload into my controller. Or, if that can't be done, how can I inject data from the response into my session and ember-data store? It didn't seem like good practice to call and insert data into the store from within the authenticate function of the authenticator.


Solution

  • The session's authenticate method doesn't resolve with a value. Check the API docs: http://ember-simple-auth.com/api/classes/SessionService.html#method_authenticate