Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

req.session has no method 'touch'?

I'm trying to use express-session with connect-redis to store user sessions. Currently, after a user logs in, I return back req.sessionID and then whenever the user wants to make a request to a secure part of the api, he/she has to provide the session ID. The authentication middleware then goes back into redis and checks to see if the session exists and if so, replace the current session with the one stored in Redis.

function isLoggedIn(req, res, next){
  var session_id = req.body.session_id;
  if (!session_id){
    res.send(401, {status: 0, message: "Not authorized"});
    return;
  }
  console.log(req);
  sessionStore.get(session_id, function(err, session){
    if(err){
      res.send(401, {status: 0, message: "Not authorized"});
      return;
    }
    req.session = session;
    req.sessionID = req.body.session_id;
    return next();
  });
}

But for some reason, this error shows up:

/node_modules/express-session/index.js:269
  req.session.touch();
              ^
TypeError: Object #<Object> has no method 'touch'

I can't seem to find anyone else online that has this error because touch() is a built in functionality to express-session. Please help? My express-session version is 1.9.3.

like image 455
josneville Avatar asked Dec 21 '14 18:12

josneville


People also ask

What is Express-session used for?

Express-session - an HTTP server-side framework used to create and manage a session middleware. This tutorial is all about sessions. Thus Express-session library will be the main focus. Cookie-parser - used to parse cookie header to store data on the browser whenever a session is established on the server-side.

What is saveUninitialized in Express-session?

saveUninitialized : When an empty session object is created and no properties are set, it is the uninitialized state. So, setting saveUninitialized to false will not save the session if it is not modified. The default value of both resave and saveUninitialized is true, but using the default is deprecated.

What is secret in Express-session?

A session secret is a key used for encrypting cookies. Application developers often set it to a weak key during development, and don't fix it during production. This article explains how such a weak key can be cracked, and how that cracked key can be used to gain control of the server that hosts the application.

How do I handle multiple sessions in node JS?

Here, since sess is global, the session won't work for multiple users as the server will create the same session for all the users. This can be solved by using what is called a session store. We have to store every session in the store so that each one will belong to only a single user.


2 Answers

I was having the same error. It seems that if you're coming from express cookie sessions, it was possible to set req.session = {/* some arbitrary session object */}. Obviously, req.session has some methods on the instance that express needs.

So, just make sure you're not explicitly overriding req.session anywhere in your code.

like image 52
John Fawcett Avatar answered Sep 24 '22 00:09

John Fawcett


Try this:

req.session.user = { 'id': 123 }; 
req.session.pageviews = 1; // This too

Font: https://davidburgos.blog/expressjs-session-error-req-session-touch-not-function/

like image 32
Renato Damázio Avatar answered Sep 27 '22 00:09

Renato Damázio