question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

POST requests doesn't save session

See original GitHub issue

Problem: 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:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chiefGuicommented, Feb 10, 2016

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!

0reactions
joewagnercommented, Feb 9, 2016

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found