All Projects → SerayaEryn → fastify-session

SerayaEryn / fastify-session

Licence: MIT license
session plugin for fastify

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to fastify-session

framework
Aplus Full-Stack Framework
Stars: ✭ 172 (+84.95%)
Mutual labels:  session
WaWebSessionHandler
(DISCONTINUED) Save WhatsApp Web Sessions as files and open them everywhere!
Stars: ✭ 27 (-70.97%)
Mutual labels:  session
create-fastify-app
An utility that help you to generate or add plugin to your Fastify project
Stars: ✭ 53 (-43.01%)
Mutual labels:  fastify
nestjs-mercurius
NestJs module to use Mercurius as GraphQL server
Stars: ✭ 38 (-59.14%)
Mutual labels:  fastify
egg-session-redis
redis store for egg session
Stars: ✭ 41 (-55.91%)
Mutual labels:  session
session
Session plugin for fastify
Stars: ✭ 52 (-44.09%)
Mutual labels:  fastify
nestjs-throttler-storage-redis
Redis storage provider for the nestjs-throttler package.
Stars: ✭ 56 (-39.78%)
Mutual labels:  fastify
InstaLite
Instagram api not official easy-to-use class, minimal number of features
Stars: ✭ 72 (-22.58%)
Mutual labels:  session
iron-session
🛠 Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, NestJs, Fastify, and any Node.js HTTP framework.
Stars: ✭ 1,729 (+1759.14%)
Mutual labels:  session
framework
Envuso is a backend framework focusing building apis using Fastify and MongoDB support.
Stars: ✭ 12 (-87.1%)
Mutual labels:  fastify
app
Aplus Framework App Project
Stars: ✭ 338 (+263.44%)
Mutual labels:  session
web-session-counter
Utility to count a user's web sessions based on the definition GA uses.
Stars: ✭ 22 (-76.34%)
Mutual labels:  session
titus
Deploy useful features in sprint one
Stars: ✭ 59 (-36.56%)
Mutual labels:  fastify
fastify-leveldb
Plugin to share a common LevelDB connection across Fastify.
Stars: ✭ 19 (-79.57%)
Mutual labels:  fastify
hospitalrun-core
All elements shared between Frontend and Backend, including CouchDB design-documents and schemas.
Stars: ✭ 36 (-61.29%)
Mutual labels:  fastify
create-fastify
Rapidly generate a Fastify project
Stars: ✭ 56 (-39.78%)
Mutual labels:  fastify
Session
PHP Session Manager (non-blocking, flash, segment, session encryption)
Stars: ✭ 23 (-75.27%)
Mutual labels:  session
nest-rest-mongo-boilerplate
🍱 backend with nest (typescript), mongoose, and authentication
Stars: ✭ 180 (+93.55%)
Mutual labels:  fastify
MSession
A simple and sophisticated session and authentication solution written in Swift
Stars: ✭ 26 (-72.04%)
Mutual labels:  session
php session
This is a SESSION class function package, use more easily, just a few simple functions can use it.
Stars: ✭ 52 (-44.09%)
Mutual labels:  session

fastify-session

Build Status Coverage Status NPM version JavaScript Style Guide

A session plugin for fastify. Requires the fastify-cookie plugin.

Install

npm install fastify-session

Usage

const fastify = require('fastify');
const fastifySession = require('fastify-session');
const fastifyCookie = require('fastify-cookie');

const app = fastify();
app.register(fastifyCookie);
app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});

Store data in the session by adding it to the session decorator at the request:

app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});
app.addHook('preHandler', (request, reply, next) => {
  request.session.user = {name: 'max'};
  next();
})

NOTE: For all unencrypted (HTTP) connections, you need to set the secure cookie option to false. Look below for all cookie options and their details.
The sessionStore decorator of the request allows to get, save and delete sessions.

app.register(fastifySession, {secret: 'a secret with minimum length of 32 characters'});
app.addHook('preHandler', (request, reply, next) => {
  const session = request.session;
  request.sessionStore.destroy(session.sessionId, next);
})

Examples

API

session(fastify, options, next)

The session plugin accepts the following options. It decorates the request with the sessionStore and a session object. The session data is stored server side using the configured session store.

options

secret (required)

The secret used to sign the cookie. Must be an array of strings, or a string with length 32 or greater.

If an array, the first secret is used to sign new cookies, and is the first one to be checked for incoming cookies. Further secrets in the array are used to check incoming cookies, in the order specified.

Note that the array may be manipulated by the rest of the application during its life cycle. This can be done by storing the array in a separate variable that is later manipulated with mutating methods like unshift(), pop(), splice(), etc. This can be used to rotate the signing secret at regular intervals. A secret should remain somewhere in the array as long as there are active sessions with cookies signed by it. Secrets management is left up to the rest of the application.

cookieName (optional)

The name of the session cookie. Defaults to sessionId.

cookie

The options object used to generate the Set-Cookie header of the session cookie. May have the following properties:

  • path - The Path attribute. Defaults to / (the root path).
  • maxAge - A number in milliseconds that specifies the Expires attribute by adding the specified milliseconds to the current date. If both expires and maxAge are set, then expires is used.
  • httpOnly - The boolean value of the HttpOnly attribute. Defaults to true.
  • secure - The boolean value of the Secure attribute. Set this option to false when communicating over an unencrypted (HTTP) connection. Value can be set to auto; in this case the Secure attribute will be set to false for HTTP request, in case of HTTPS it will be set to true. Defaults to true.
  • expires - The expiration date used for the Expires attribute. If both expires and maxAge are set, then expires is used.
  • sameSite- The boolean or string of the SameSite attribute. Using Secure mode with auto attribute will change the behaviour of the SameSite attribute in http mode. The SameSite attribute will automatically be set to Lax with a http request. See this link.
  • domain - The Domain attribute.
store

A session store. Needs the following methods:

  • set(sessionId, session, callback)
  • get(sessionId, callback)
  • destroy(sessionId, callback)

Compatible to stores from express-session.

Defaults to a simple in memory store.
Note: The default store should not be used in a production environment because it will leak memory.

saveUninitialized (optional)

Save sessions to the store, even when they are new and not modified. Defaults to true. Setting this to false can be useful to save storage space and to comply with the EU cookie law.

idGenerator (optional)

Function used to generate new session IDs. Defaults to uid(24).

request.session

Allows to access or modify the session data.

request.destroySession(callback)

Allows to destroy the session in the store

Session#touch()

Updates the expires property of the session.

Session#regenerate()

Regenerates the session by generating a new sessionId.

fastify.decryptSession(sessionId, request, next)

This plugin also decorates the fastify instance with decryptSession in case you want to decrypt the session manually.

const { sessionId } = fastify.parseCookie(cookieHeader);
const request = {}
fastify.decryptSession(sessionId, request, () => {
  // request.session should be available here
})

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].