matthew-andrews / isomorphic-fetch

Isomorphic WHATWG Fetch API, for Node & Browserify
MIT License
6.95k stars 289 forks source link

Handling cookies on server side requests #83

Closed Fenykepy closed 8 years ago

Fenykepy commented 8 years ago

Hello, I like very well what you did with isomorphic-fetch. Actually I build an universal app with a rest backend API which relies on a jwt stored in a cookie for authentication. When fetch requests are send client side, document.cookie is available and that works correctly, but when prerendering on the server, there is no more document.cookie and all requests remain unauthenticated.

Is there any workarround to handle sending cookies for server side request ?

Regards.

lionng429 commented 8 years ago

@Fenykepy It is indeed not a problem of isomorphic-fetch. You pointed out that there is no document.cookie available on the server (it would be from req.headers.cookie if you are using express). Code your own policy to transfer the cookie from client-side-request to server-side-request, and the problem will be solved.

P.S. unsure if it would raise any security issue

Fenykepy commented 8 years ago

@lionng429 Thanks for your answer. I finally handled the problem picking token from req.headers.cookie in express, then storing it in my redux state, and I put it in headers.Authorization for each request I made. It know works client and server side.

Thank you.

I think I can close this issue.

genefedorchenko commented 6 years ago

Fenykepy, thanks, you saved my life. Even without redux i have followed your algorithm and it worked. To take cookie: let cookie = req.headers.cookie; To set it in fetch's second argument: options.headers.Cookie = cookie;

It works perfectly good and now i have a free weekend! Thanks!