Connect.utils.parseSignedCookies deprecated, cookie storage using Express and Redis

I read Building Scalable Apps with Redis and Node.js and there is code in everything that doesn't work or is deprecated. I am trying to use cookie storage with the code provided to me:

var io = require('socket.io'),
  connect = require('connect'),
  cookie = require('cookie'),
  expressSession = require('express-session'),
  ConnectRedis = require('connect-redis')(expressSession),
  redis = require('redis'),
  config = require('../config'),
  redisAdapter = require('socket.io-redis'),
  redisSession = new ConnectRedis({host: config.redisHost, port: config.redisPort});

var socketAuth = function socketAuth(socket, next){
  var handshakeData = socket.request;
  var parsedCookie = cookie.parse(handshakeData.headers.cookie);
  console.log(parsedCookie);



  var sid = connect.utils.parseSignedCookies(parsedCookie['connect.sid'], config.secret);

  if (parsedCookie['connect.sid'] === sid)
    return next(new Error('Not Authenticated'));

  redisSession.get(sid, function(err, session){
    if (session.isAuthenticated)
    {
      socket.user = session.user;
      socket.sid = sid;
      return next();
    }
    else
      return next(new Error('Not Authenticated'));
  });
};

When I launch the application, I get an error message:

Cannot call method 'parseSignedCookies' of undefined

I am sure 95% of the problem starts here:

  var sid = connect.utils.parseSignedCookies(parsedCookie['connect.sid'], config.secret);

I'm sure the problem is that Connect is no longer used by Express, but what alternatives do I have to parse cookies? I may have to completely rewrite the code and figure out what to do, but if anyone has any clever ideas, please let me know.

+4
source share
1

, cookie-parser . , :

var cookieParser = require('cookie-parser');

var sid = cookieParser.signedCookie(parsedCookie['connect.sid'], config.secret);
+7

All Articles