在每个ajax请求上设置访问令牌 [英] setting the access token on every ajax request
问题描述
我正在使用新版本的ember-simple-auth,它不会自动将访问令牌添加到发送到服务器的ajax请求中。
I'm using the new version of ember-simple-auth, which doesn't automatically anymore add access token to ajax requests that are sent to the server.
我使用oauth2身份验证,并且由于一个糟糕的文档,我以某种方式无法弄清楚为什么会在哪里以及在哪里设置每个ajax请求的标题标记。
I'm using oauth2 authentication, and due to a bad documentation I somehow cannot figure out what and where it would be the right way to set the header token for each ajax request that I do.
这个代码应该在自定义授权器的授权功能或其他地方吗?
Should this code be under the authorize function of the custom authorizer or somewhere else?
this.get('session').authorize('authorizer:some-authorizer', (headerName, headerValue) => {
xhr.setRequestHeader(headerName, headerValue);
});
非常感谢任何关于正确设置的信息!
Any information on setting up this correctly would be highly appreciated!
推荐答案
我们在应用程序中使用自定义适配器,适配器看起来像这样
We use a custom adapter in our application the adapter looks something like this
import DS from "ember-data";
import Ember from "ember";
import App from '../app';
export
default DS.RESTAdapter.extend({
namespace: 'data',
host: App.hostUrl,
ajax: function (url, type, hash) {
hash = hash || {};
hash.headers = hash.headers || {};
hash.headers['Authorization'] = 'Token token=' + App.access_token;
hash.crossDomain = true;
return this._super(url, type, hash);
}
});
所有其他型号的适配器都将此适配器扩展为例如。
Every other model adapter used extends this adapter for ex.
import ApplicationAdaper from './application';
export default ApplicationAdaper.extend({
...
});
有关参考和进一步的信息检查RESTAdapter标题定制
http://emberjs.com/api/data/classes/DS.RESTAdapter.html
For reference and further Information Check RESTAdapter Header Customization http://emberjs.com/api/data/classes/DS.RESTAdapter.html
这篇关于在每个ajax请求上设置访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!