我正在使用Ember-simple-auth并尝试将数据从我的自定义身份验证器返回到执行身份验证的控制器中。
在我的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
函数并从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){
并且该函数成功地执行了响应,但simple-auth现在认为它未能通过授权。
我希望能够将响应有效负载传递给我的控制器。或者,如果无法完成,我如何将响应中的数据注入到我的会话和ember-data存储中?从验证者的身份验证功能中调用数据并将数据插入到商店中似乎不是一种好的做法。
答案 0 :(得分:3)
会话的authenticate
方法无法解析值。查看API文档:http://ember-simple-auth.com/api/classes/SessionService.html#method_authenticate
答案 1 :(得分: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)
传递给它的所有信息。