Skip to content
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

Open
bracke opened this issue Sep 26, 2018 · 5 comments

Comments

@bracke
Copy link

bracke commented Sep 26, 2018

remote.defaultFetchSettings.headers['Authorization'] = Bearer ${access_token};

@dgeb
Copy link
Member

dgeb commented Dec 6, 2018

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 initFetchSettings method to inspect the current session data and set the Authorization header accordingly.

For example, take a look at this app's data-sources/remote.js:

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);
  }
};

@tchak
Copy link
Member

tchak commented May 25, 2019

We should move this content to guides. Contributions in doing so are welcome!

@dgeb
Copy link
Member

dgeb commented May 30, 2019

I should point out that, in the v0.16 beta, initFetchSettings is now on the new JSONAPIRequestProcessor class, which can be extended and passed in as an argument when creating JSONAPISource:

https://github.com/orbitjs/orbit/blob/master/packages/%40orbit/jsonapi/src/jsonapi-source.ts#L53

@cmitz
Copy link

cmitz commented Jan 2, 2020

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: it seems I cannot access Ember stuff in here. But man, this is becoming a bit of a headache :p

Edit 2: I've managed to get it working, the code above works!
I'm using Ember Octane (3.15.0) and Ember-Orbit 0.16.4.

@RobbieTheWagner
Copy link
Contributor

Did anyone document this? This is very useful info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants