【问题标题】:Ember RSVP Promise resolve always returns undefined valueEmber RSVP Promise 解析总是返回未定义的值
【发布时间】:2015-11-13 01:54:59
【问题描述】:

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

在我的身份验证器/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 中的第二个函数始终具有正确的有效负载。

我尝试反转解决/拒绝:

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

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

函数成功携带响应,但 simple-auth 现在认为它的授权失败。

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

【问题讨论】:

    标签: ember.js promise ember-simple-auth


    【解决方案1】:

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

    【讨论】:

    • 我明白了。这就解释了。不过,它似乎确实打破了预期的语法。感谢您在 Simple-auth 上所做的工作。是否有如何设计自定义身份验证器的示例?您之前发布到示例的所有链接都不再有效。找出一个简单但自定义的身份验证器的最佳实践是很棘手的。
    【解决方案2】:

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

    所以,authenticators/custom.js 中的 authenticate 函数

      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) 传递给它的所有信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2022-01-14
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多