POST requests doesn't save session
See original GitHub issueProblem: when app.post('/user')
, I am able to req.session.isLoggedIn = true
but req.session.isLoggedIn
is undefined
in every place I call for it.
Expected: to save isLoggedIn
to be able to use wherever I want.
Attempts:
- I already tried to
req.session.save()
after defining the session’s variable and didn’t work as well.
Code:
import express from 'express'
import session from 'express-session'
import cookieParser from 'cookie-parser'
const app = express()
const PORT = 3000
app.use(cookieParser())
app.use(session({secret: 'atomicbomb'}))
app.get('/', (req, res) => {
res.send('home ' + req.session.isLoggedIn) // always undefined
})
app.post('/user/authenticate', (req, res) => {
req.session.isLoggedIn = true
res.sendStatus(200)
})
app.listen(PORT, () => {
console.log(`Listening at :${PORT}`)
})
There are a plenty of topics out there with different solutions, but none of them worked for me and that’s why I am creating this one.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
session does not save after fetch post request with express
Im making a post request with fetch and saving a token to session in an express api backend. However, the token wont be...
Read more >Using cookies
To manage cookies in Postman, open a request, then select Cookies (under Send). ... The Manage Cookies window displays a list of domains...
Read more >7 Keys to the Mystery of a Missing Cookie
7 Keys to the Mystery of a Missing Cookie · 1. SameSite attribute Defaults to Lax · 2. withCredentials is not Set to...
Read more >Postman request does not keep session value in .net core
Hi AfaqKanwarMuhammad-6424,. Since the postman will not keep the session inside the cookie, you should get the session id and then store it...
Read more >How to use sessions
The local-memory cache backend doesn't retain data long enough to be a good choice ... Unlike other session backends which keep a server-side...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yo, @JoeWagner! It was my bad, actually. I’m so sorry for this.
Basically, what I was missing is:
.withCredentials()
to go with my request to the server. I’m using the fantastic superagent and it has an option built-in to deal with that.So, problem solved—a mistake of mine.
Thanks!
@chiefGui I’m not sure I understand your issue, but if you are using different domains then the cookie will not be sent. Cookies follow the same origin policy, you can read about this here
As I said above, The code you provided works perfectly well for me. If you want to provide code that demonstrates the issue, please include all versions and how you are running and testing. If you can do this someone here will definitely help you out.