lundi 27 février 2017

Using custom authenticator with ember-simple-auth and promises returning undefined for resolve

I am using Ember.js and ember-simple-auth.

I have a custom authenticator named apps\authenticators\jwt.js with a method called authenticate:

  authenticate(credentials) {
    const { identification, password, secret } = credentials;

    const data = JSON.stringify({
      name: identification,
      password: password,
      secret: secret
    });

    const requestOptions = {
      async: true,
      crossDomain: true,
      url: 'http://ift.tt/2mELxsW',
      processData: false,
      method: 'POST',
      contentType: 'application/json',
      data: data,
    };

    return new Promise(function(resolve, reject) {
      Ember.$.ajax(requestOptions).then(function(data) {
        Ember.run(null, resolve, data);
        console.log(data); 
      }, function(jqXHR , textStatus, errorThrown) {
        jqXHR.then = null;
        Ember.run(null, reject, jqXHR, textStatus, errorThrown);
      });
    });
  },

and the controller for my login.hbs template:

import Ember from 'ember';

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    authenticate() {
      var credentials = this.getProperties('identification', 'password', 'secret'),
      authenticator = 'authenticator:jwt';

      this.getProperties('identification', 'password', 'secret');
      this.get('session').authenticate(authenticator, credentials).then(function(result) {
        console.log(result);
      }, function(err) {
        console.log(err);
      });
    }
  }
});

If I successfully fill out the form and submit it I get a status code 200 and the proper response from the server from the line console.log(data); in the authenticate method in jwt.js But the line console.log(result); in the controller returns undefined.

However, if I fail to fill the form out and submit it I get the proper response from the server from the authenticate method AND I get that message from the console.log() in the controller.

My goal is to get the response on success to the template.

I can't seem to figure out why this is happening, any help is appreciated.

Edit: I am able to achieve my goal using localStorage however I would like to figure out the issue with the promise.




Aucun commentaire:

Enregistrer un commentaire