ember 简单的身份验证回调问题

ember simple auth authenticate callback issue

我正在使用 ember 简单身份验证来制作一个使用 ember 的自定义身份验证系统,一切正常,但有一个问题,无法将数据从身份验证承诺回调传递到登录控制器。

代码如下:

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

控制器代码在这里

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

有人可以帮我吗?

Session 将身份验证委托给 Authenticatorauthenticaterestore…),但总是解析 returning 没有值。

这意味着在您的 Controller 示例中,已解析的承诺永远不会有任何参数。

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) {
    /* */
  });

这里检查 Session 源代码很清楚:https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/lib/simple-auth/session.js#L158

您可以使用自定义 Session 对象并覆盖 authenticate 方法以使其 return 具有您需要的值。

正如@damienc 指出的那样,会话的 authenticate 解析为没有任何值。但是,您可以通过会话的 secure 属性 访问身份验证器解析的所有内容,例如this.get('session.secure.token')。因此,您只需将代码更改为

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