【发布时间】: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