Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - Session doesn't persist through res.redirect()

I'm (almost) successfully using Node.js with Express and Redis to handle sessions.

The problem I'm having is that the session is not kept when I use res.redirect().

Here is how I can see it :

req.session.username = username.toString();
console.log(req.session);
res.redirect('/home');

The console.log() prints :

{ lastAccess: 1322579131762,
  cookie:
   { path: '/',
     httpOnly: true,
     _expires: Tue, 29 Nov 2011 15:06:31 GMT,
     originalMaxAge: 60000 },
  username: 'admin' }

Now, here is the following code :

app.get('/home', [app.requireLogin], function(req, res, next) {
// Not showing the rest as it's not even getting there
// Instead, here is what's interesting
app.requireLogin = function(req, res, next) {
  console.log(req.session);

This console.log() prints out this :

{ lastAccess: 1322579131775,
  cookie:
   { path: '/',
     httpOnly: true,
     _expires: Tue, 29 Nov 2011 15:06:31 GMT,
     originalMaxAge: 60000 } }

Clearly, the 'username' object has disappeared. The session has not kept it, and just rebuilt a new one.

How can I solve this? Don't hesitate if you need any information.

Here is the code where I set the session management :

app.configure(function() {
  // Defines the view folder and engine used.
  this.set('views', path.join(__dirname, 'views'));
  this.set('view engine', 'ejs');

  // Allow parsing form data
  this.use(express.bodyParser());

  // Allow parsing cookies from request headers
  this.use(express.cookieParser());
  // Session management
  this.use(express.session({
    // Private crypting key
    secret: 'keyboard cat',
    store: new RedisStore,
    cookie: {
      maxAge: 60000
    }
  }));
  this.use(app.router);
});

Here is the whole project (I mean, parts of it), on gist : https://gist.github.com/c8ed0f2cc858942c4c3b (ignore the properties of the rendered views)

like image 434
Florian Margaine Avatar asked Dec 01 '11 16:12

Florian Margaine


People also ask

What does res redirect() function do?

The res. redirect() function redirects to the URL derived from the specified path, with specified status, a integer (positive) which corresponds to an HTTP status code. The default status is “302 Found”.

What is res redirect Node js?

The res. redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below. const app = require('express')(); // The `res.

How do I redirect in node JS?

redirect() function, we can now discuss how to redirect back to original URL in NodeJS. Back redirect: We can use this method to redirects the request back to the referrer. If no referrer is present, the request is redirected to “/” route by default.

How to store session in Express?

Information associated with the client is stored on the server linked to this ID. We will need the Express-session, so install it using the following code. We will put the session and cookie-parser middleware in place. In this example, we will use the default store for storing sessions, i.e., MemoryStore.


3 Answers

Alright, I found the solution. The problem is that the time in maxAge was added to the current date. So, in the browser side, the cookie was set to expire at the GMT time shown.

The problem was the following : I use a virtual machine to test node.js, and, you know... sometimes, you suspend your machine.

Well, what happened is that the machine's time was two days late. So, whenever the cookie was set on the server side, the client side thought the cookie was already expired, since my host machine was not two days late.

Another stupid outcome.

like image 120
Florian Margaine Avatar answered Sep 26 '22 03:09

Florian Margaine


Did you try with different browsers ? Are you keeping the same session id between page redirects ?

You could add req.session.cookie.expires = false; before redirecting...

like image 44
Romain Meresse Avatar answered Sep 26 '22 03:09

Romain Meresse


Your code looks pretty solid, but is there a reason you're using client.end()? It forcibly closes the redis connection and is not clean. I don't think you need it at all:

https://github.com/mranney/node_redis/issues/74

I am not sure about the underlying architecture for connect-redis, but I'm wondering if calling client.end is what's resetting your sessions. What happens if you take those out?

like image 39
hross Avatar answered Sep 27 '22 03:09

hross