[英]ember simple auth authenticate callback issue
I am using ember simple auth to make a custom auth system using ember, everything works fine expect one issue, can't pass data from authenticate promise callback to login controller. 我正在使用ember简单身份验证以使用ember创建自定义身份验证系统,一切正常,但预料到一个问题,无法将数据从身份验证答应回调传递给登录控制器。
code below: 下面的代码:
//the login goes here
authenticate: function(options) {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax({
type: "POST",
url: apis.login,
data: JSON.stringify({
username: options.identification,
password: options.password
})
}).then(function(response) {
// check the login status to check the status
if (response.status === "success") {
//pass the response in resolve callback
Ember.run(function() {
resolve(response);
});
} else {
//login failed, pass the response in reject callback
Ember.run(function() {
reject(response);
});
}
}, function(xhr, status, error) {
Ember.run(function() {
reject(xhr.responseJSON || xhr.responseText);
});
});
});
},
and the controller code here 还有这里的控制器代码
actions: {
// display an error when authentication fails
authenticate: function() {
var _this = this;
var credentials = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:custom', credentials).then(
function(message) {
//the issus happens here, the message here is undefined
console.log(message);
_this.get('session').set('username', message.username);
}, function(message) {
// but the reject callback works fine, the message is the right one
console.log(message);
_this.set('errorMessage', message.msg);
_this.set('identification', '');
_this.set('password', '');
});
}
}
could someone help me with that? 有人可以帮我吗?
The session's authenticate
resolves with no value as @damienc points out. @damienc指出,会话的
authenticate
没有任何价值。 However, you can access everything the authenticator resolves with via the session's secure
property, eg this.get('session.secure.token')
. 但是,您可以通过会话的
secure
属性访问身份验证器解析的所有内容,例如this.get('session.secure.token')
。 So you can simply change your code to 因此,您只需将代码更改为
actions: {
authenticate: function() {
var _this = this;
var credentials = this.getProperties('identification', 'password');
this.get('session').authenticate('authenticator:custom', credentials).then(
function() {
//the issus happens here, the message here is undefined
console.log(_this.get('session.secure.whatever'));
_this.get('session').set('username', message.username);
}, function(error) {
// but the reject callback works fine, the message is the right one
console.log(error);
_this.set('errorMessage', error.msg);
_this.set('identification', '');
_this.set('password', '');
});
}
}
The Session
delegates the authentication to the Authenticator
( authenticate
, restore
…) but will always resolve returning no value. Session
将身份验证委托给Authenticator
( authenticate
, restore
…),但始终会解析为不返回任何值。
This means that in your Controller
example, the resolved promise will never have any parameter. 这意味着在您的
Controller
示例中,已解析的Promise将永远没有任何参数。
this.get('session')
.authenticate('authenticator:custom', credentials)
.then(function(/* there are parameters here, see session implementation */) {
_this.get('session').set('username', message.username);
}, function(message) {
/* */
});
This is clear inspecting Session
source code here: https://github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/lib/simple-auth/session.js#L158 在这里检查
Session
源代码很明显: https : //github.com/simplabs/ember-simple-auth/blob/master/packages/ember-simple-auth/lib/simple-auth/session.js#L158
You could use a custom Session
object and override the authenticate
method to have it return the value you need. 您可以使用自定义的
Session
对象并覆盖authenticate
方法,以使其返回所需的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.