不知道有關簡單身份驗證令牌,但我使用的餘燼,簡單權威性和我加入令牌延伸的基礎授權各自發出的請求,如下圖所示:
// app/authorizers/my-own-authorizer.js
import Base from 'ember-simple-auth/authorizers/base';
export default Base.extend({
/**
* Authorizes all outgoing requests by a session-token that has been received during a login process.
* If such a session token does not exist, it does not add anything.
* @override
* @param {Object} sessionData received from the backend on a successful login
* @param {Function} addHeaderFunction function that appends a custom header into the next request
*/
authorize(sessionData, addHeaderFunction){
const sessionToken = Ember.get(sessionData, "meta.session-token");
if (Ember.isPresent(sessionToken)) {
addHeaderFunction('authorization', sessionToken);
}
}
});
不要忘記調整您的應用程序適配器,以便它使用授權人:
// app/adapters/application.js
...
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
....
authorizer: 'authorizer:my-own-authorizer',
...
}
非常感謝您的回答,但我正在尋找更簡單的東西。我終於發現我可以通過調用self.get(「session.data.authenticated.auth_token」)來訪問令牌。 – Jack
我曾經這樣做過,但docs明確地討論了擴展authorizer和重寫'authorize()'函數。我發現現在的代碼更具可讀性,因爲我不必將不屬於他們的問題混合在一起。 – Pavol
使用ember-simple-auth-token我只需要使用DataAdapterMixin。其他的一切都是自動處理的...... – acorncom