Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-session

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-session

Simple promise-based session middleware for Next.js

  • 3.0.0-next.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.4K
decreased by-40.77%
Maintainers
1
Weekly downloads
 
Created
Source

next-session

npm minified size CircleCI codecov PRs Welcome

Simple promise-based session middleware for Next.js.

** This is WIP **.

Installation

// NPM
npm install next-session
// Yarn
yarn add next-session

Usage

:point_right: Upgrading from v1.x to v2.x? Please read the release notes here!

:point_right: Upgrading from v2.x to v3.x? Please read the release notes here!

const session = require('next-session');

app.use(session(opts));


API

session(options)

options

next-session accepts the properties below.

optionsdescriptiondefault
nameThe name of the cookie to be read from the request and set to the response.sid
storeThe session store instance to be used. You must specify the store to be used. Several stores can be found in lib/stores.null (required)
genidThe function that generates a string for a new session ID.crypto.randomBytes(16).toString('hex')
rollingForce the cookie to be set on every request despite no modification, which extends the life time of the cookie in the browserfalse
touchAfterOnly touch the session store after an amount of time, except when the session is modified, to decrease database load. Setting the value to -1 will disable touch().0 (Touch every time)
cookie.secureSpecifies the boolean value for the Secure Set-Cookie attribute.false
cookie.httpOnlySpecifies the boolean value for the httpOnly Set-Cookie attribute.true
cookie.pathSpecifies the value for the Path Set-Cookie attribute./
cookie.domainSpecifies the value for the Domain Set-Cookie attribute.unset
cookie.sameSiteSpecifies the value for the SameSite Set-Cookie attribute.unset
cookie.maxAgeSpecifies the value for the Max-Age Set-Cookie attribute.unset (Session)

req.session

This allows you to set or get a specific value that associates to the current session.

//  Set a value
if (loggedIn) req.session.user = 'John Doe';
//  Get a value
const currentUser = req.session.user; // "John Doe"
req.session.destroy()

Destroy to current session and remove it from session store.

if (loggedOut) req.session.destroy();
req.session.id

The unique id that associates to the current session.

Session Store

The session store to use for session middleware (see options above).

Unlike libraries such as express-session, next-session does not default to a store. You must specify a session store or use one from next-session/lib/stores.

Implementation

A compatible session store must extend from ./src/store and include three functions: set(sid), get(sid), and destroy(sid). The function touch(sid, session) is recommended. The store may emit store.emit('disconnect') or store.emit('connect') to inform its readiness.

All functions should return Promises (callbacks are not supported). For an example of a session store implementation, see MemoryStore.

Stores that use callbacks will be promisified using util.promisify.

Compatible stores

Make a PR to add your own compatible stores here.

Contributing

Please see my contributing.md.

License

MIT

Keywords

FAQs

Package last updated on 15 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc