我正在使用ember-simple-auth来管理我的应用验证。
我已经实现了自己的身份验证器,授权器和适配器。
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);
},
});
我想知道如何在身份验证过程中获取存储的令牌,在适配器内部将编码的令牌注入头Authorization
。
还有一个问题,它何时被称为授权人?
答案 0 :(得分:2)
我假设你正在使用ESA 1.0或更高版本。您不会在授权程序的jqXHR
方法中获得authorize
,而是会话数据以及回调。
查看DataAdapterMixin
和BaseAuthorizer
以及README的文档以获取更多信息。