Skip to content

bgressier/token-session

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

exports.Store

Expose constructors.

warning

Warning message for MemoryStore usage in production.

session()

Session:

Setup session store with the given `options`.

Examples:

  connect()
    .use(connect.json())
    .use(connect.session()

Options:

- `store` session store instance
 - `logger` optional logger provided by [log4js-node](https://github.com/nomiddlename/log4js-node)

req.session

To store or access session data, simply use the request property req.session, which is (generally) serialized as JSON by the store, so nested objects are typically fine. For example below is a user-specific view counter:

    connect()
      .use(connect.favicon())
      .use(connect.json())
      .use(connect.session()
      .use(function(req, res, next){
        var sess = req.session;
        if (sess.views) {
          res.setHeader('Content-Type', 'text/html');
          res.write('<p>views: ' + sess.views + '</p>');
          res.end();
          sess.views++;
        } else {
          sess.views = 1;
          res.end('welcome to the session demo. refresh!');
        }
      }
    )).listen(3000);

Session#destroy()

Destroys the session, removes req.session.

   req.session.destroy(function(err){
     // cannot access session here
   });

Session#reload()

Reloads the session data.

   req.session.reload(function(err){
     // session updated
   });

Session#save()

Save the session.

   req.session.save(function(err){
     // session saved
   });

Session#touch()

Updates the .maxAge property. Typically this is not necessary to call, as the session middleware does this for you.

Session Store Implementation:

Every session store must implement the following methods

  • .get(sid, callback)
  • .set(sid, session, callback)
  • .destroy(sid, callback)

Recommended methods include, but are not limited to:

  • .length(callback)
  • .clear(callback)

For an example implementation view the token-session-redis repo.

About

Simple token-based sessions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%