-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
req.isAuthenticated occasionally fails - race condition? #306
Comments
Actually I think my issue may be related to the redirect and my use of express: expressjs/session#69 |
@benheymink was it the redirect issue you mention in your comment? I'm experiencing the same problem. |
@ChristopherGS The issue was that req.logIn is async, and hadn't finished flushing to the session store by the time the next request had come in for the redirect resource. I got around it by manually saving the season before the redirect:
Hope this helps! |
@benheymink thanks for the explanation - will give this a try. Like you say, it's a tricky one because it only shows up about 1 in 10 times. |
We also had such issues. We ran into the same issues , after a few redirects, the req.user=undefined currentLng set to: en-US currentLng set to: en-US currentLng set to: en-US currentLng set to: en-US currentLng set to: en-US currentLng set to: en-US |
Could it be related to this issue? That is why I am seeing it happening on some machines consistently and not on other machines. funny thing is if I run my app with node debug app.js, the issue cannot be reproduced. |
@smartmouse You are probably seeing it on some machines and not others as the saving of the session is ASYNC. When you debug your app you subtly change the timings of your functions, which is perhaps why it now works. If you want to confirm, try the work around I proposed above, manually saving the session before doing any post-authentication redirect:
The other issue you linked to is unrelated I believe. |
Just spent the better part of the last 3 days trying to figure out what was going on. Exact same issue using Postgres as session store. Intermittently the log in redirect would fail even though the user had authenticated correctly i.e. he would again see the log in screen, but if he typed manually a privileged page, he would see it just fine. All this with [email protected], [email protected], [email protected] and [email protected] Thanks for the temp solution @benheymink . The forced session.save() workaround seems to work though not sure it resolves the problem 100% of the time. Just stopped seeing failures in a stretch of N consecutive log ins when earlier there were some. |
@tkalfigo Weird, it should save it 100% of the time. Are you sure you are only finishing the request/performing a redirect AFTER the session.save callback has completed? Doing the following won't work:
Make sure you are finishing the request in the callback:
|
@benheymink I didn't say it doesn't work 100% of the time. I meant that even though I see it working correctly now, I cannot be sure it is full proof. Admittedly, have been working with it the past 2 weeks without problems. |
FYI - Just experienced this problem (September 2017) and this fix still works! |
@benheymink how do you prevent the req.logIn method from attempting to save the session twice? |
If anyone is using cookie-session, the above/following might not work (as the cookie-session's save() does not support callbacks):
Managed to get it to work by setting some value on the session before redirecting:
|
Hi, unfortunately all the fixes described above did not solve the problem. I ended up using https://github.com/expressjs/cookie-session. It worked out of the box. I did not have to change a single line of code except for the config. I realized it's a better fit for this thread: #314 |
2022 and redirect as a callback to login fixed this same issue |
I have set up passport to use a custom authentication scheme, but for arguments sake, imagine it authenticates any user to the app. I have a 'landing' page with a login button that issues a post, handled as follows:
later, a route handler matches the redirect:
9 times out of ten, this all works fine, but occasionally req.isAuthenticated will return false. If I put some logging in, I can see that before the redirect I have a valid req.user object, but then in the second route handler following the redirect, req.user is undefined. Sometimes it works, sometimes it doesn't! (When it does work, req.user IS defined in the second route handler) Is this issue the same as others have reported around the user not being serialised correctly?
The text was updated successfully, but these errors were encountered: