Reputation: 239
I'm using ember simple-auth with ember simple-auth-token and I would like to take the value of the user's token in order to use it in an ajax call. How can I access token value? I tried the following syntax:
headers: {"Authorization":"Token" + " auth_token"}
and
headers: {"Authorization":"Token" + " session.data"}
but none of them have worked.
Upvotes: 0
Views: 287
Reputation: 1220
Not sure about simple-auth-token, but I am using ember-simple-auth and I am adding the token to each outgoing request extending a base authorizer as shown below:
// 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);
}
}
});
Do not forget to adjust your application adapter so that it uses the authorizer:
// 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',
...
}
Upvotes: 1