Req.session doesn't have a "touch" method?

I am trying to use express session with connect-redis to store user sessions. Currently, after the user logs in, I go back req.sessionID

and then whenever the user wants to make a request to the protected part of the api, he has to provide the session id. Then the middleware authenticator goes back to redis and checks 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 appears:

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

      

I can't find anyone else on the net that has this error because touch () is built-in functionality for express session. Please help? My express session version is 1.9.3.

+3


source to share


3 answers


I had the same error. It looks like if you come from express cookie sessions, you can set req.session = {/* some arbitrary session object */}

. Obviously req.session

has some methods for an instance that expresses needs.



So, just make sure you don't explicitly override req.session

anywhere in your code.

+8


source


Try the following:

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

      



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

+1


source


I managed to solve this problem by using cookie-parser middleware before using cookie session middleware:

var express = require('express')
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');

var app = express();
app.use(cookieParser());
app.use(cookieSession());

      

Tested on Express 4

0


source







All Articles