https://github.com/wobsoriano/h3-session
Session middleware for h3 and Nuxt apps.
https://github.com/wobsoriano/h3-session
h3 middleware nuxt session
Last synced: 3 months ago
JSON representation
Session middleware for h3 and Nuxt apps.
- Host: GitHub
- URL: https://github.com/wobsoriano/h3-session
- Owner: wobsoriano
- License: other
- Created: 2022-06-02T07:20:20.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-17T18:23:09.000Z (almost 3 years ago)
- Last Synced: 2025-04-15T02:20:14.255Z (6 months ago)
- Topics: h3, middleware, nuxt, session
- Language: TypeScript
- Homepage:
- Size: 175 KB
- Stars: 18
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# h3-session
[](https://www.npmjs.com/package/h3-session)
Add session support in h3 and Nuxt apps using [express-session](https://github.com/expressjs/session).
## Installation
```bash
npm install h3-session
```## Usage with h3
```ts
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
```ts
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['h3-session/nuxt'],
session: {
secret: 'keyboard cats',
resave: true,
saveUninitialized: true,
cookie: { secure: true },
}
})
``````ts
// ~/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
```ts
export default defineEventHandler((event) => {
await event.context.session.regeneratePromisified()
// will have a new session hereawait event.context.session.reloadPromisified()
// session updatedawait event.context.session.savePromisified()
// session savedawait event.context.session.destroyPromisified()
// cannot access session here
})
```Visit the [express-session docs](https://github.com/expressjs/session#sessionoptions) to see the complete session configuration.
## License
MIT