fastify / fastify-secure-session

Create a secure stateless cookie session for Fastify
MIT License
208 stars 48 forks source link

Multiple cookies #162

Closed dominik-korsa closed 1 year ago

dominik-korsa commented 2 years ago

Prerequisites

🚀 Feature Proposal

Add support for different "instances" of the session plugin, with different cookie names and cookie options (different path/expiration date). This could be accomplished by allowing to specify the decorated field name (request.session by default) in plugin options.

Please keep in mind this will require changing the way types work, as the SessionData is currently set for the entire @fastify/secure-session module

Motivation

I need to keep a separate session for short-term (data expiring after closing tab) and long-term storage (user login token)

Example

const SecureSession = require('@fastify/secure-session');

fastify.register(SecureSession, {
  cookieName: 'long-term-cookie',
  cookie: {
    path: '/'
    expires: new Date('2137-01-01'),
  },
  field: 'longTermSession'
});
fastify.register(SecureSession, {
  cookieName: 'short-term-cookie',
  cookie: {
    path: '/'
    expires: undefined,
  },
  field: 'shortTermSession'
});

fastify.get('/', (request) => {
  console.log(request.longTermSession);
  console.log(request.shortTermSession);
});
Uzlopak commented 2 years ago

@dominik-korsa

Check out #163

simoneb commented 1 year ago

Closing as per previous comment