Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wobsoriano/nuxt-iron-session
Stateless session support for Nuxt apps
https://github.com/wobsoriano/nuxt-iron-session
h3 module nuxt session vue
Last synced: about 1 month ago
JSON representation
Stateless session support for Nuxt apps
- Host: GitHub
- URL: https://github.com/wobsoriano/nuxt-iron-session
- Owner: wobsoriano
- Created: 2022-11-01T03:46:34.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-21T06:39:29.000Z (over 1 year ago)
- Last Synced: 2024-10-02T08:52:05.453Z (about 2 months ago)
- Topics: h3, module, nuxt, session, vue
- Language: TypeScript
- Homepage: nuxt-iron-session.vercel.app
- Size: 190 KB
- Stars: 22
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nuxt-iron-session
[![Version](https://img.shields.io/npm/v/nuxt-iron-session?style=flat&colorA=000000&colorB=000000)](https://www.npmjs.com/package/nuxt-iron-session)
🛠Add stateless session support for Nuxt apps using signed and encrypted cookies. Powered by [iron-session](https://github.com/vvo/iron-session).
The session data is stored in encrypted cookies ("seals"). And only your server can decode the session data. There are no session ids, making iron sessions "stateless" from the server point of view.
Demo https://nuxt-iron-session.vercel.app
## Installation
```bash
npm install nuxt-iron-session
```## Usage
```ts
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxt-iron-session'],
session: {
cookieName: 'yourapp_cookiename',
password: 'complex_password_at_least_32_characters_long',
cookieOptions: {
secure: process.env.NODE_ENV === 'production'
}
}
})
```### API Routes
```ts
// ~/server/api/login.ts
export default defineEventHandler((event) => {
// get user from database then:
event.context.session.user = {
id: 69,
admin: true,
}
await event.context.session.save()
return { ok: true }
})
``````ts
// ~/server/api/user.ts
export default defineEventHandler((event) => {
return { user: event.context.session.user }
})
``````ts
// ~/server/api/logout.ts
export default defineEventHandler((event) => {
await event.context.session.destroy()
return { ok: true }
})
```### Components
```html
const { ssrContext } = useNuxtApp()
// Available in client and server. Unique to each request.
const session = useState('session', () => ssrContext?.event?.context?.session)```
## Typing session data with TypeScript
```ts
declare module 'iron-session' {
interface IronSessionData {
user?: {
id: number
admin?: boolean
}
}
}
```## Usage with h3
```ts
import { createIronSessionMiddleware } from 'nuxt-iron-session/middleware'const app = createApp()
app.use(createIronSessionMiddleware({}))
app.use('/api/user', eventHandler((event) => ({ user: event.context.session.user })))
```Visit the [iron-session docs](https://github.com/vvo/iron-session) to see the complete configuration.
## Development
- Run `npm run dev:prepare` to generate type stubs.
- Use `npm run dev` to start [playground](./playground) in development mode.## License
MIT