Home

Awesome

redsess

Yet another redis session thing for node.

This is built on top of jed/cookies. You can optionally pass in a KeyGrip instance, or an array of keys to use to sign cookies.

Breaking Changes in 1.0.0

Sessions are now stored as stringified JSON objects using set and get. This means that all sessions that were created using previous versions of redsess will need to be cleared out/nuked/obliterated. Before upgrading to 1.0.0, let your users know that their sessions will be removed!

Example

var RedSess = require('redsess')
, http = require('http')
, Cookies = require('cookies')
, Keygrip = require('keygrip')
, keys = new Keygrip(['some secret keys here'])

// Create a client with the options that you'd pass to node_redis
RedSess.createClient(redisOptions)

http.createServer(function (req, res) {
  var session = new RedSess(req, res, {
    keys: keys, // if keys are provided, they'll be used
    cookieName: 's',
    expire: expirationInSeconds, // default = 2 weeks
    client: redisClient, // defaults to RedSess.client
    keys: [ "this is a string key" ], // will be made into a keygrip obj
    keys: new KeyGrip(keys), // this way also works
  })

  // you can decorate like this if you chose
  req.session = session
  res.session = session

  // .. and then some time later ..
  req.session.get('auth', function (er, auth) {
    if (!auth) {
      // redirect to login page
    } else {
      // do authorized login things
    }
  })

  // .. on the login page, if there's a post ..
  validateLogin(postedData, function (er, isValid) {
    if (isValid)
      req.session.set('auth', postedData)
  })

  // .. on the logout page ..
  req.session.del('auth', function (er) {
    // user is now logged out
  })
}).listen(1337)

Constructor Options

Methods

Callbacks are all the standard cb(er, result) style.

Deep objects are supported, but cycles in data objects will cause terrible explosively bad awful undefined behavior, so DON'T DO THAT.

Calls redis.createClient with the supplied options. See node_redis for more details. (opts.host and opts.port are passed to redis.createClient as positional arguments, not on the configuration object.)

If there's an opts.auth member, then it will use that string as a password to redis.

Sets a key on the session.

Sets a hash of keys and values on the session.

Fetches the key from the session.

Fetches all keys from the session. If there is no data in the session, then it'll return null.

Deletes a key from the session.

Deletes the entire session.