Ember RSVP Promise 始终解决 returns 未定义的值

Ember RSVP Promise resolve always returns undefined value

我正在使用 Ember-simple-auth 并尝试 return 来自我的自定义身份验证器的数据返回到执行身份验证的控制器中。

在我的 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 + ']');

                  }
                }
            }

在我的登录控制器中:

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

但我真的很想能够处理 resolve 函数并从 authenticate 承诺中获取它的 value 有效负载。

如果我将 .catch 更改为 .then,响应函数会被成功调用,但它的负载始终有一个未定义的值:

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

    );    
    }

即使我重构承诺,重新安排函数的调用方式,RSVP 中的第一个函数也已成功调用,但具有未定义的负载。 RSVP 中的第二个函数始终具有正确的有效负载。

我试过反转 resolve/reject:

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

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

并且该函数成功携带了响应,但 simple-auth 现在认为其授权失败。

我希望能够将响应负载传递到我的控制器中。或者,如果无法做到这一点,我如何将响应中的数据注入我的会话和 ember-data 存储?从身份验证器的身份验证功能中调用数据并将数据插入存储中似乎不是好的做法。

会话的 authenticate 方法没有解析值。查看 API 文档:http://ember-simple-auth.com/api/classes/SessionService.html#method_authenticate

为了处理认证路由的响应,我使用了函数sessionAuthenticated来处理返回的数据。

因此,authenticate 中的函数 authenticators/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('200 response: \r',this.response);
                    resolve(this.response);
                  } else {
                    console.log('failure response',this.response);
                      reject(this.response);
                  }
                }
            }
        });

  },

sessionAuthenticated() 事件发生在 routes/application.js:

sessionAuthenticated: function(){
    var session = this.get('session');
    var data = session.get('data');
    var response = data.authenticated;
    console.log('sessionAuthenticated()',response);
},
sessionInvalidated: function(){
    console.log('sessionInvalidated()',response);
},

sessionAuthenticated 函数中,response 变量包含验证器内部 authenticate(response) 传递给它的所有信息。