[英]How can I get the session stored in a Ember-Simple-Auth session?
I'm using ember-simple-auth to manage my app authentication. 我正在使用ember-simple-auth来管理我的应用程序身份验证。
I have implemented my own authenticator, authorizer and adapter. 我已经实现了自己的身份验证器,授权者和适配器。
import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';
export default Base.extend({
tokenEndpoint: 'http://localhost:9000/0/auth/',
restore: function(data) {
console.log("restore");
console.log(data);
return new Ember.RSVP.Promise(function(resolve, reject) {
if (!Ember.isEmpty(data.token)) {
resolve(data);
} else {
reject();
}
});
},
authenticate: function(options) {
return new Ember.RSVP.Promise((resolve, reject) => {
Ember.$.ajax({
url: this.tokenEndpoint + options.method,
type: 'POST',
data: JSON.stringify(options.data),
contentType: 'application/json',
dataType: 'json'
}).then(function(response) {
console.log("OK!");
console.log(response);
Ember.run(function() {
console.log("resolve: "+response.data.encodedToken);
resolve({
token: response.data.encodedToken
});
});
}, function(xhr, status, error) {
var response = xhr.responseText;
console.log("ERROR");
console.log(response);
Ember.run(function() {
reject(response);
});
});
});
},
invalidate: function() {
console.log('invalidate...');
//return Ember.RSVP.resolve();
Ember.$.ajax({
type: 'POST',
url: this.tokenEndpoint + 'logout',
}).then(() => {
resolve(true);
}, () => {
reject();
});
}
});
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
export default Base.extend({
authorize: function(jqXHR, requestOptions) {
console.log(requestOptions);
console.log("---- Authorize ----");
var accessToken = this.get('session.content.secure.token');
console.log(this.get('session'));
if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
jqXHR.setRequestHeader('Authorization', accessToken);
}
}
});
import Ember from 'ember';
import JSONAPIAdapter from 'ember-data/adapters/json-api';
import singularize from 'ember-inflector';
export default JSONAPIAdapter.extend({
namespace: '0',
host: 'http://localhost:9000',
session: Ember.inject.service('session'),
headers: Ember.computed('session.token', function() {
console.log("Sending header...");
return {
'Authorization': 'MYTOKEN'
};
}),
pathForType: function(type) {
return Ember.String.underscore(type);
//return singularize(type);
},
});
I would like to know how to get the stored token in the authentication process, inside the adapter to inject the encoded token in the header Authorization
. 我想知道如何在身份验证过程中获取存储的令牌,在适配器内部将编码的令牌注入标头
Authorization
。
And another question, when is it called the authorizer? 还有一个问题,什么时候称为授权者?
I assume you're using ESA 1.0 or later. 我假设您使用的是ESA 1.0或更高版本。 You don't get the
jqXHR
in the authorizer's authorize
method then but the session data along with a callback instead. 然后,您没有在授权者的
authorize
方法中获得jqXHR
,而是会话数据和回调。
Check the docs for the DataAdapterMixin
and BaseAuthorizer
as well as the README for more info. 检查文档中的
DataAdapterMixin
和BaseAuthorizer
以及自述文件以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.