2015-11-13 31 views
3

我正在使用Ember-simple-auth并试图从我的自定义身份验证器返回数据回到执行身份验证的控制器中。Ember RSVP Promise解决方法总是返回undefined值

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

        } 
       } 
      } 

,并在我的登录控制器:

在我的认证/ custom.js

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功能,得到它的value来自authenticate承诺的有效载荷。

如果我改变.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){ 

和功能成功地进行了回应,但简单的-AUTH现在认为它已经失败了授权。

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

回答

2

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

+0

我明白了。这解释了它。它似乎打破了预期的语法。感谢您在Simple-auth上的工作。有没有一个如何设计自定义验证器的例子?您之前发布到示例的所有链接都不再有效。找出一个简单但自定义的身份验证器的最佳实践很困难。 – jtf

2

为了处理来自认证路由的响应,我使用了函数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)验证器内部。