Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect.utils.parseSignedCookies deprecated, cookie storage with Express and Redis

I'm reading Building Scalable Apps with Redis and Node.js and throughout there is code that doesn't work or is deprecated. I'm trying to use cookie storage with the code supplied 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 run the app, I get an error that says:

Cannot call method 'parseSignedCookies' of undefined

I'm 95% sure the problem begins here:

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

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

like image 408
Les Paul Avatar asked Jan 10 '23 01:01

Les Paul


1 Answers

It seems the module cookie-parser did the trick. Here is the code I added:

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

var sid = cookieParser.signedCookie(parsedCookie['connect.sid'], config.secret);
like image 122
Les Paul Avatar answered Jan 16 '23 21:01

Les Paul