ssess

0.4.0 • Public • Published

ssess

Simple session middleware

Now less of an incoherent mess! Call today!

Install npm install ssess

Example

var ssess = require('ssess');
var manager = ssess(); // Create a session manager
 
middlewareStack.push(manager); // Use in your middleware stack
 
function login(req, res, user) {
    var userSession = manager.create(res); // Pass res to allow cookie setting
    userSession.set('user', user);
}
 
function userPage(req, res) {
    var user = req.session.get('user');
    // Display something using the user data
    req.session.set('ticketNumber', Math.random()); // Set some other data for this session
}
 
function logout(req, res) {
    req.session.destroy(res); // Needs res to delete cookies
}

API

ssess([options])

{
    ttl: 500, // Seconds to keep session alive without a refresh, defaults to 2 weeks
    name: 'deadbeef', // The cookie name to use for keeping track of sessions, defaults to 'sid'
    auto: true // If true, automatically create a session for every visitor, defaults to false
}
  • Returns a new session manager, shown below

manager.create(res[, options])

  • res is the response object, used to send the cookie to the client
  • options is like the options object above, but only supports custom ttl for individual sessions
  • Returns an individual visitor session, shown below

session

  • get(key)

  • set(key, value[, expire])

    • Has an alias, put
    • expire is in seconds
  • del(key)

  • clear()

    • Clears all data and expiration timers from the session
    • See thecache documentation for more details on get/set/del/clear
  • destroy(res)

    • Need res to kill the cookie on the client
    • Delete contents of session and remove itself from the manager
  • id

    • Just in case the session is passed to something that needs to know it's id
    • This is the value of the cookie sent to the client, and the key used to lookup this session in the manager

Note: Adds cookies and session properties to req

Readme

Keywords

none

Package Sidebar

Install

npm i ssess

Weekly Downloads

4

Version

0.4.0

License

none

Last publish

Collaborators

  • download