import 'server-only' import { getIronSession } from 'iron-session' import { cookies } from 'next/headers' import type { SessionData } from '@/types' function getSessionOptions() { const sessionSecret = process.env.SESSION_SECRET if (!sessionSecret || sessionSecret.length < 32) { throw new Error('SESSION_SECRET must be set and at least 32 characters long') } return { password: sessionSecret, cookieName: 'flockoff-session', cookieOptions: { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax' as const, maxAge: 60 * 60 * 24 * 30, // 30 days }, } } export async function getSession() { const cookieStore = await cookies() return getIronSession(cookieStore, getSessionOptions()) } export async function requireSession() { const session = await getSession() if (!session.did) { throw new Error('Not authenticated') } return session as { did: string; handle?: string } }