diff --git a/patterns/iron-session.md b/patterns/iron-session.md index 41da259..f979904 100644 --- a/patterns/iron-session.md +++ b/patterns/iron-session.md @@ -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(await cookies(), sessionOptions) +if (!session.userId) redirect('/login') +if (session.isDemo) return { error: 'Niet beschikbaar in demo-modus' } +```