fastify/fastify-secure-session

Multiple cookies

Closed this issue ยท 2 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

๐Ÿš€ 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);
});

Closing as per previous comment