h3-session
v0.2.5
Published
Session middleware for h3 and Nuxt apps.
Downloads
157
Maintainers
Readme
h3-session
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