Giter Site home page Giter Site logo

cookie-sessions's Introduction

Cookie-Sessions

Secure cookie-based session middleware for Express.

Session data is stored on the request object in the 'session' property:

  var app = require('express');
  var cookieParser = require('cookie-parser');
  var cookieSessions = require('cookie-sessions');

  app.use(
    cookieSessions({
      name: 'session_data',
      secret: process.env.SECRET
    })
  );

The cookie-parser middleware MUST also used.

The session data can be any JSON object. It's timestamped, encrypted and authenticated automatically. The authenticated encryption uses aes-256-gcm offered by the node crypto library. The httpOnly and secure cookie flags are set by default.

The main function accepts a number of options:

Option Required Description Default
secret Yes The secret to encrypt the session data.
timeout Yes The amount of time in milliseconds before the cookie expires. 24 hours
name Yes The cookie name in which to store the session data. \_node
path Yes The path to use for the cookie. /
domain No Define a specific domain/subdomain scope for the cookie.
autoRenew No Boolean: if true, a new cookie will be set in each response with an updated expiration Date.now() + timeout true
httpOnly No Boolean: if true, the httpOnly cookie flag will be set. true
secure No Boolean: if true, the secure cookie flag will be set. true
sameSite No If set to "lax" or "strict", the sameSite cookie flag with the corresponding mode will be set.
sessionCookie No Boolean: if true, it's considered a session cookie and no "expires" is set.

Why store session data in cookies?

  • Its fast, you don't need to hit the filesystem or a database to look up session data
  • It scales easily. You don't need to worry about sticky-sessions when load-balancing across multiple nodes.
  • No server-side persistence requirements

Caveats

  • You can only store 4k of data in a cookie
  • Higher-bandwidth requirements, since the cookie is sent to the server with every request.

In summary: don't use cookie storage if you keep a lot of data in your sessions!

Migrating to version 1.0.0

  • Any cookie created with 0.0.2 version will be invalidated.
  • The options object has two naming changes:
    • name instead of session_key
    • sessionCookie instead of session_cookie
  • The following exported functions have been removed:
    • readSession
    • readCookies
    • checkLength
    • headersToArray
    • hmac_signature

cookie-sessions's People

Contributors

ashtuchkin avatar dmcquay avatar esarafianou avatar gkwang avatar lloyd avatar marcinhoppe avatar mwilliamson avatar richmarr avatar snyk-bot avatar sre-57-opslevel[bot] avatar woloski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.