Comments (6)
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.
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.
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.
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.
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.
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)
- Update history version HOT 1
- Minimum universal react API HOT 2
- Can't run server HOT 2
- app/index.js: Unexpected token (2:7) HOT 4
- Axios example HOT 1
- State set in Routes.js is overwritten in Routes.js
- HTTP 200 is sent on page not found HOT 5
- Hot reload not working (windows 8.1) HOT 2
- Immutable HOT 2
- componentDidMount & duplicate data ... HOT 4
- Add to redux docs HOT 1
- What about styles? HOT 1
- Built error: "BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix when using loaders. You need to specify 'babel-loader' instead of 'babel'." HOT 2
- Error HOT 1
- SASS implementation example
- Unable to add styles HOT 7
- Had to downgrade to React-Router v3.0.5 HOT 1
- Possible issue with declaring production environments HOT 3
- Getting Code Splitting to work HOT 6
- Adding universal CSS import support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from universal-react.