-
-
Notifications
You must be signed in to change notification settings - Fork 873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forwarding client's cookies to API when calling from on server #37
Comments
@asadm it should be sending the cookies automatically (if you're doing a cross request make sure to add |
Feel free to send a PR with axios or fetch etc. |
@jaredpalmer I haven't figured out how to solve the problem yet, but I can try to work on a PR once it's fixed. 😅 |
I'm getting the same error with |
@jaredpalmer Sadly, not at all. This has to do with universal rendering, passport works fine. It also works fine to send requests from the browser or |
I think the easiest way to work around this issue is to return a token and store that in the state, then later authenticate requests with the token. Would be nice if client/server shared the correct user session automatically, though 😄 |
This might be a related issue: http://stackoverflow.com/a/12735003/702288 |
can you link to an example repo? also are you using express-session? |
@jaredpalmer Unfortunately I can't share the code, but it's not hard to add Yes I am using |
@jaredpalmer I just cloned a fresh instance of your boilerplate and added |
Ok, nevermind my issue seems to have been unrelated to this 😊 I just found out that it couldn't serialize the user because the data was sent via |
I still have the problem that the session doesn't seem to work correctly. I changed one request to be (for testing purposes): callAPI: () => http.post('http://localhost:3003/login', {
email: 'test@test.com',
password: 'test'
}).then(data => {
console.log('login', data)
return http.get('http://localhost:3003/user')
.then(data => console.log('user data:', data))
}), The output of this is:
If I do the |
I kinda solved it this way: get: (cookies, path) => new Promise((resolve, reject) => {
request
.get(getUrl(path))
.accept('application/json')
.set("Cookie",cookies)
.end((err, res) => {
if (err) {
if (err.status === 404) {
resolve(null);
} else {
reject(err);
}
} else {
resolve(res.body);
}
});
}), Does this make sense? |
@asadm good idea! I was gonna solve this by storing a login token in the redux store, but this is pretty much the same concept using cookies. I'll try this out in a bit 😁 |
@omnidan I have done this approach and it's pretty similar to the one @asadm provided. getWithToken: (path, token) => new Promise((resolve, reject) => {
request
.get(getUrl(path))
.set('X-User-Token', token)
.accept('application/json')
.end((err, res) => {
if (err) {
reject(err);
} else {
resolve(res.body);
}
});
}) |
Sorry if this is a noob question. What happens when the API call needs user's session. I assume those session cookies won't be forwarded to the API endpoint when the rendering happens on server-side?
Any idea on how should I forward user's cookies to the API endpoint. I think it will be somewhere inside HttpClient.js?
Thanks
The text was updated successfully, but these errors were encountered: