vikingmute
vikingmute

Reputation: 412

ember simple auth authenticate callback issue

I am using ember simple auth to make a custom auth system using ember, everything works fine expect one issue, can't pass data from authenticate promise callback to login controller.

code below:

  //the login goes here
  authenticate: function(options) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        type: "POST",
        url: apis.login,
        data: JSON.stringify({
          username: options.identification,
          password: options.password
        })
      }).then(function(response) {
        // check the login status to check the status
        if (response.status === "success") {
          //pass the response in resolve callback
          Ember.run(function() {
            resolve(response);
          });
        } else {
          //login failed, pass the response in reject callback
          Ember.run(function() {
            reject(response);
          });
        }
      }, function(xhr, status, error) {
        Ember.run(function() {
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });
  },

and the controller code here

  actions: {
    // display an error when authentication fails
    authenticate: function() {
      var _this = this;
      var credentials = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:custom', credentials).then(
      function(message) {
        //the issus happens here, the message here is undefined
        console.log(message);
        _this.get('session').set('username', message.username);
      }, function(message) {
        // but the reject callback works fine, the message is the right one
        console.log(message);
        _this.set('errorMessage', message.msg);
        _this.set('identification', '');
        _this.set('password', '');
      });
    }
  }

could someone help me with that?

Upvotes: 0

Views: 416

Answers (2)

marcoow
marcoow

Reputation: 4062

The session's authenticate resolves with no value as @damienc points out. However, you can access everything the authenticator resolves with via the session's secure property, e.g. this.get('session.secure.token'). So you can simply change your code to

actions: {
  authenticate: function() {
    var _this = this;
    var credentials = this.getProperties('identification', 'password');
    this.get('session').authenticate('authenticator:custom', credentials).then(
    function() {
      //the issus happens here, the message here is undefined
      console.log(_this.get('session.secure.whatever'));
      _this.get('session').set('username', message.username);
    }, function(error) {
      // but the reject callback works fine, the message is the right one
      console.log(error);
      _this.set('errorMessage', error.msg);
      _this.set('identification', '');
      _this.set('password', '');
    });
  }
}

Upvotes: 1

dashdashzako
dashdashzako

Reputation: 1346

The Session delegates the authentication to the Authenticator (authenticate, restore…) but will always resolve returning no value.

This means that in your Controller example, the resolved promise will never have any parameter.

this.get('session')
  .authenticate('authenticator:custom', credentials)
  .then(function(/* there are parameters here, see session implementation */) {
    _this.get('session').set('username', message.username);
  }, function(message) {
    /* */
  });

This is clear inspecting Session source code here: https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/lib/simple-auth/session.js#L158

You could use a custom Session object and override the authenticate method to have it return the value you need.

Upvotes: 0

Related Questions