/opt/canhelp/apps/api/src/middleware
NameSizeModeActions
auth.ts13530644editdlrm
plan.ts28900644editdlrm
Edit: /opt/canhelp/apps/api/src/middleware/auth.ts (1353B)
import { createMiddleware } from 'hono/factory' import { auth, type AuthUser, type Session } from '../auth.js' export type AuthVariables = { user: AuthUser session: Session } export const requireAuth = createMiddleware<{ Variables: AuthVariables }>( async (c, next) => { const session = await auth.api.getSession({ headers: c.req.raw.headers }) if (!session) { return c.json({ error: 'Unauthorized' }, 401) } c.set('user', session.user) c.set('session', session.session) await next() }, ) export const requireAdmin = createMiddleware<{ Variables: AuthVariables }>( async (c, next) => { const session = await auth.api.getSession({ headers: c.req.raw.headers }) if (!session) { return c.json({ error: 'Unauthorized' }, 401) } if ((session.user as AuthUser & { role: string }).role !== 'admin') { return c.json({ error: 'Forbidden' }, 403) } c.set('user', session.user) c.set('session', session.session) await next() }, ) export const optionalAuth = createMiddleware<{ Variables: Partial }>( async (c, next) => { try { const session = await auth.api.getSession({ headers: c.req.raw.headers }) if (session) { c.set('user', session.user) c.set('session', session.session) } } catch {} await next() }, )