Is there an easy way to share session data stored in Redis between Rails and the Node.js application?

I have a Rails 3.2 application that uses Redis as a session repository. Now I'm going to write some of the new features in Node.js, and I want to be able to share session information between two applications.

What I can do manually read the cookie _session_idand then read from the Redis key with the name rack:session:session_id, but it looks like a hack ish solution.

Is there a better way to exchange sessions between Node.js and Rails?

+5
source share
2 answers

I did it, but it requires creating my own forks of things

-, . .

redis-store , . json , - javascript . ,

. . , , . , > middleware > session out .

, , rails. , , node, generateCookie.

/***** ORIGINAL *****/
// session hashing function
store.hash = function(req, base) {
  return crypto
    .createHmac('sha256', secret)
    .update(base + fingerprint(req))
    .digest('base64')
    .replace(/=*$/, '');
};

// generates the new session
store.generate = function(req){
  var base = utils.uid(24);
  var sessionID = base + '.' + store.hash(req, base);
  req.sessionID = sessionID;
  req.session = new Session(req);
  req.session.cookie = new Cookie(cookie);
};

/***** MODIFIED *****/
// session hashing function
store.hash = function(req, base) {
  return crypto
    .createHmac('sha1', secret)
    .update(base)
    .digest('base64')
    .replace(/=*$/, '');
};

// generates the new session
store.generate = function(req){
  var base = utils.uid(24);
  var sessionID = store.hash(req, base);
  req.sessionID = sessionID;
  req.session = new Session(req);
  req.session.cookie = new Cookie(cookie);
};

// generate a new cookie for a pre-existing session from rails without session.cookie
// it must not be a Cookie object (it breaks the merging of cookies)
store.generateCookie = function(sess){
  newBlankCookie = new Cookie(cookie);
  sess.cookie = newBlankCookie.toJSON();
};

//... at the end of the session.js file
  // populate req.session
  } else {
    if ('undefined' == typeof sess.cookie) store.generateCookie(sess);
    store.createSession(req, sess);
    next();
  }

, . , .

-, json. , . Flash- , json . - , flash-. .

+2

All Articles