-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Where would I best add the Authorisation header with the bearer token to a remote source? #166
Comments
Apologies for the delay in answering. If you have some endpoints that are public and others that are authenticated, I'd recommend a strategy that overrides the source's For example, take a look at this app's import JSONAPISource, { JSONAPISerializer } from '@orbit/jsonapi';
import Orbit from '@orbit/data';
import fetch from 'fetch';
import { getOwner } from '@ember/application';
import { isPresent } from '@ember/utils';
export default {
create(injections = {}) {
const app = getOwner(injections);
const session = app.lookup('service:session');
class RemoteSource extends JSONAPISource {
initFetchSettings(customSettings = {}) {
let settings = super.initFetchSettings(customSettings);
if (session.isAuthenticated) {
let { token } = session.data.authenticated;
if (isPresent(token)) {
settings.headers['Authorization'] = `Bearer ${token}`;
}
}
return settings;
}
}
class Serializer extends JSONAPISerializer {
}
Orbit.fetch = fetch;
injections.name = 'remote';
injections.namespace = 'api/v1';
injections.SerializerClass = Serializer;
return new RemoteSource(injections);
}
}; |
We should move this content to guides. Contributions in doing so are welcome! |
I should point out that, in the v0.16 beta, https://github.com/orbitjs/orbit/blob/master/packages/%40orbit/jsonapi/src/jsonapi-source.ts#L53 |
So, if I understand correctly, your example file would now look like this: import RemoteSource, { JSONAPIRequestProcessor } from '@orbit/jsonapi';
import { getOwner } from '@ember/application';
import { isPresent } from '@ember/utils';
export default {
create(injections = {}) {
class RemoteRequestProcessor extends JSONAPIRequestProcessor {
initFetchSettings(customSettings = {}) {
let settings = super.initFetchSettings(customSettings);
const app = getOwner(injections);
const session = app.lookup('service:session')
if (session.isAuthenticated) {
let { access_token } = session.data.authenticated;
if (isPresent(access_token)) {
settings.headers['Authorization'] = `Bearer ${access_token}`;
}
}
return settings;
}
}
injections.name = 'remote';
injections.host = 'http://localhost:3000';
injections.namespace = 'api/v1';
injections.RequestProcessorClass = RemoteRequestProcessor;
return new RemoteSource(injections);
}
};
Edit 2: I've managed to get it working, the code above works! |
Did anyone document this? This is very useful info! |
remote.defaultFetchSettings.headers['Authorization'] =
Bearer ${access_token};
The text was updated successfully, but these errors were encountered: