Search code examples
javascriptauthenticationember.jsember-simple-auth

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?


Solution

  • 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', '');
        });
      }
    }