Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express session not persisting

I'm trying to set up a basic session system in node. Here's what I've got so far:

app.js:

app.use(express.cookieParser('stackoverflow'));
app.use(express.session());

I'm setting the session data in ajax.js:

addClassToCart: function(req, res) {
  req.session.cart = req.body.classId;
  console.log(req.session.cart);
}

This logs the correct information. However, when I try to retrieve that information elsewhere (same file, different function):

console.log(req.session.cart);

I get undefined. I feel like I'm missing something incredibly basic. Various tutorials for this are either awful or require me to add in even more packages (something I'm trying to avoid).

More data from my debugging:

  • This works with non-AJAX requests
  • The session is set before it's logged.
like image 818
SomeKittens Avatar asked Nov 17 '12 01:11

SomeKittens


People also ask

What is Express session resave?

resave : It basically means that for every request to the server, it reset the session cookie. Even if the request was from the same user or browser and the session was never modified during the request.

How Express sessions work?

By default, express-session creates a new MemoryStore instance for storing session data in server. However, in production, it is not recommended to use the default memory store, as mentioned in the official documentation. We should use other modules, such as connect-redis , a Redis-based session store.

What is secret in Express session?

Express-session options and how to use them secret - a random unique string key used to authenticate a session. It is stored in an environment variable and can't be exposed to the public. The key is usually long and randomly generated in a production environment.

Where is express session stored?

To store confidential session data, we can use the express-session package. It stores the session data on the server and gives the client a session ID to access the session data.


2 Answers

As it turns out, the issue wasn't with Express' session (as the other answers seem to think). Rather, it was a misunderstanding on my part. I changed addClassToCart to the following:

addClassToCart: function(req, res) {
    req.session.cart = req.body.classId;
    console.log(req.session.cart);
    res.send('class added');
  }

Adding res.send() fixed the problem.

like image 75
SomeKittens Avatar answered Oct 22 '22 07:10

SomeKittens


As noted in the answer to a related SO question, this can also occur if you're using fetch to get data from your server but you don't pass in the credentials option:

fetch('/addclasstocart', {
    method: 'POST',
    credentials: 'same-origin' // <- this is mandatory to deal with cookies
})

Cookies won't be passed to the server unless you include this option which means the request's session object will be reset with each new call.

like image 32
Chase Sandmann Avatar answered Oct 22 '22 07:10

Chase Sandmann