h3-session

Session middleware for h3 and Nuxt apps.
6,324
Install
npm install h3-session
Latest Version:0.2.5
License:MIT
Last Updated:Nov 17, 2022
Links: npm
Maintainer: wobsoriano

h3-session

Version

Add session support in h3 and Nuxt apps using express-session.

Installation

npm install h3-session

Usage with h3

import { createApp } from 'h3'
import { createSessionHandler } from 'h3-session'

const app = createApp()

app.use(createSessionHandler({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))

Usage with Nuxt 3

// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['h3-session/nuxt'],
  session: {
    secret: 'keyboard cats',
    resave: true,
    saveUninitialized: true,
    cookie: { secure: true },
  }
})
// ~/server/api/hello.ts
export default defineEventHandler((event) => {
  // Get the session ID:
  console.log(event.context.session.id)

  // Assign some value to session:
  event.context.session.someKey = 'some value'
})

Promisified session methods

export default defineEventHandler((event) => {
  await event.context.session.regeneratePromisified()
  // will have a new session here

  await event.context.session.reloadPromisified()
  // session updated

  await event.context.session.savePromisified()
  // session saved

  await event.context.session.destroyPromisified()
  // cannot access session here
})

Visit the express-session docs to see the complete session configuration.

License

MIT

Related Packages

nuxt-session

Add session support in Nuxt.js, accessible in server middleware

18,130 18
nuxt-define

One utility function for module authors to define compiler constants for all bui...

13,412,511
@zadigetvoltaire/nuxt-well-known

Nuxt module to add well-known files and rules with middlewares

37,344 22
@lg2/nuxt3-module-create-dato-map

nuxt3-module-create-dato-map

2,869 0
nuxt-csurf

Nuxt Cross-Site Request Forgery (CSRF) Prevention

6,263,746 91

Version History

Version Released License
0.2.5 MIT
0.2.4 MIT
0.2.3 MIT
0.2.2 MIT
0.2.1 MIT