denvned/isomorphic-relay

Authenticating with Isomorphic Relay

papigers opened this issue · 4 comments

Hey, I've been using cookies for token-based authentication, as the source for my token.
To include the token automatically, I use the known fetch option:

const networkLayer = new Relay.DefaultNetworkLayer(GRAPHQL_URL, {
    credentials: 'same-origin',
});

This works great for the client which uses the regular fetch which uses this option. but server-side uses node-fetch which doesn't knows this option and doesn't send the cookies automatically. This causes to the cookies to be missing on my graphql-express middleware.

This is solvable using this ugly solution:

// The signle route which utilizes isomorphoic relay router etc...
app.get('*', (req, res, next) => {
  const networkLayer = new Relay.DefaultNetworkLayer(GRAPHQL_URL, {
    headers:{
      cookie: req.headers.cookie,
    },
  });

So basically every request I'm creating a new network layer with the cookies attached. Pretty ugly.

Nothing you can do really, but I guess it should be acknowleged.

You should not use fetch directly, but some factory which wraps fetch on the server. This factory should be injected for example in react context or relay environment

I'm not using fetch directly, relay does under the hood. It uses isomorphic fetch which uses node fetch on the server side, and regular fetch on the client's.

Aha :-)

I updated the README.