Skip to content

Instantly share code, notes, and snippets.

@eleventigers
Last active August 29, 2015 13:56
Show Gist options
  • Save eleventigers/9178879 to your computer and use it in GitHub Desktop.
Save eleventigers/9178879 to your computer and use it in GitHub Desktop.
Redis and Rendr

To load sessions into the Rendr app I used connect-redis from visionmedia. Whatever I set on req.session will be stored in a Redis store.

In a custom data-adapter that communicates with our API, I send relevant cookies through the request method. So for example when my data-adapter tries to authenticate with the API on success it should receive a response with sessions cookies set by Deployed. What I do is I save those cookies into req.session and the next time data-adapter does a request I take them out of the cookie jar and put them on top of the request - Deployed links this request with a session on Rendr in this way. Remember that a custom data-adapter can be augmented by any express middleware that comes before it, whether it sets stuff on req.session or else.

This is what I wrote when I needed to pass cookies around, it probably sucks but hey it worked at that time:

if(req.session){
   jar = request.jar();
   if(req.session[sessCookiePath]){
     jar.cookies = req.session[sessCookiePath];
   }
   cookieString = _.map(_.filter(jar.cookies, function(c){return !!(c.name);}), function (c) {
     return c.name + "=" + c.value;
   }).join("; ");
   api.headers.cookie = cookieString;
   api.jar = jar;
}

Once I get response from the API I do:

req.session[sessCookiePath] = _.clone(jar.cookies);

I guess a better way would be not to store every cookie returned from the API but just one that is used to authenticate with it. Your session could have a field with apiSessionAuthToken or similar and you send it when needed.

@cengizoner
Copy link

Hi Jokubas,

I have just written a sample app that uses Deployd, Rendr, and a Redis session store. If you have time, it would be nice to read your opinion about it. The app is at: https://github.com/cengizoner/rendr/tree/master/examples/08_deployd

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment