GithubHelp home page GithubHelp logo

Comments (6)

DominicTobias avatar DominicTobias commented on August 16, 2024

Hi & thanks @guzmo, fetch behaves a little differently to say jQuery.ajax:

By default, fetch won't send any cookies to the server, resulting in unauthenticated requests if the site relies on maintaining a user session.

So to include cookies in the request you should follow this:

https://github.com/github/fetch#sending-cookies

For a request to the same domain:

fetch('/users', {
  credentials: 'same-origin'
})

Or credentials: 'include' for cross-domain requests.

from universal-react.

guzmo avatar guzmo commented on August 16, 2024

Thanks for the quick response :)

Ye, Im currently using 'same-origin' and it works for my requests to my backend.
I started to debug express-stormpath now and it seems the cookie actually gets there :O I'll try to find out where it fails.

from universal-react.

guzmo avatar guzmo commented on August 16, 2024

Doh, it actually isn't there. It will run the middleware on first call and there the access_token is. But when we then are calling /me from the server the access_token is gone...

I'm using routeProps.requestState(). the Request state returns the promise from /me.
I guess I need to forward the users access_token to my servers request to /me (which is calling itself).

from universal-react.

DominicTobias avatar DominicTobias commented on August 16, 2024

Not sure exactly what you mean, did you figure it out? Is the issue that the server is not sending the cookie on some of the requests? I'm closing this but feel free to carry on the discussion, though I'm not sure if I can help but I'll try ;)

from universal-react.

guzmo avatar guzmo commented on August 16, 2024

Ye exactly. When using stormpath you get a route called /me to retrieve user information. It will return 401 unauthorised if the "user" isn't logged in.
The user is logged in and the access_token is sent to the server but when the server calls fetch('/me') it gets unauthorised because the server doesn't send cookies. I have no clue how to fix this :P
So right now, even if logged in, I have to relogin in my app since the client uses the servers fetch.

I can skip the server rendering and it will work but I'd like to keep it.

from universal-react.

keyeh avatar keyeh commented on August 16, 2024

I've got universal cookies to work using cookie-parser and react-cookie

Ex.
In server.js

at the top add var cookieParser = require('cookie-parser');

and after
server.use(express.static(path.resolve(__dirname, 'dist')));
add
server.use(cookieParser());

And then in action creator or component
import reactCookie from 'react-cookie';
and
const theCookie = reactCookie.load('name-of-cookie');

from universal-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.