Document iron-session pattern and usage
Add documentation for iron-session pattern including session options and usage in server actions.
This commit is contained in:
parent
08e0dd22f0
commit
fd72fd85f8
1 changed files with 18 additions and 0 deletions
|
|
@ -1,3 +1,8 @@
|
|||
# Patroon: iron-session
|
||||
|
||||
## lib/session.ts
|
||||
|
||||
```ts
|
||||
import { SessionOptions } from 'iron-session'
|
||||
|
||||
export interface SessionData {
|
||||
|
|
@ -14,3 +19,16 @@ export const sessionOptions: SessionOptions = {
|
|||
sameSite: 'lax',
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
## Gebruik in Server Action of Route Handler
|
||||
|
||||
```ts
|
||||
import { getIronSession } from 'iron-session'
|
||||
import { cookies } from 'next/headers'
|
||||
import { SessionData, sessionOptions } from '@/lib/session'
|
||||
|
||||
const session = await getIronSession<SessionData>(await cookies(), sessionOptions)
|
||||
if (!session.userId) redirect('/login')
|
||||
if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' }
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue