denvned / isomorphic-relay

Adds server side rendering support to React Relay
BSD 2-Clause "Simplified" License
242 stars 53 forks source link

Authenticating with Isomorphic Relay #49

Closed papigers closed 8 years ago

papigers commented 8 years ago

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.

langpavel commented 8 years ago

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

papigers commented 8 years ago

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.

langpavel commented 8 years ago

Aha :-)

denvned commented 8 years ago

I updated the README.