我正在使用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.jsauthenticate() {
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存储中?从认证者的身份验证功能中调用数据并将其插入商店似乎不是一种好的做法。
我明白了。这解释了它。它似乎打破了预期的语法。感谢您在Simple-auth上的工作。有没有一个如何设计自定义验证器的例子?您之前发布到示例的所有链接都不再有效。找出一个简单但自定义的身份验证器的最佳实践很困难。 – jtf