Skip to Content
Auth recipesAuth.js and Better Auth

Auth.js and Better Auth

lib/auth.ts
import { betterAuth } from 'better-auth'; import { createAuthMiddleware, APIError } from 'better-auth/api'; import { verifyInvite, reportActivation } from '@/lib/waitlistly'; export const auth = betterAuth({ // …your config… hooks: { before: createAuthMiddleware(async (ctx) => { if (ctx.path !== '/sign-up/email') return; const invite = await verifyInvite(ctx.body?.inviteCode ?? ''); if (!invite.valid) throw new APIError('FORBIDDEN', { message: 'This invite is not valid or has expired.' }); }), after: createAuthMiddleware(async (ctx) => { if (ctx.path === '/sign-up/email' && ctx.context.newSession) { await reportActivation(ctx.body.inviteCode); } }), }, });
Last updated on