// Supabase Auth "Before User Created" hook that checks the new user's email with SeeReal. // The logic lives here (no Deno globals) so it can be tested; index.ts wires it to Deno.serve. import { Webhook } from "npm:standardwebhooks@1.0.0"; export interface SeeRealResult { action: "allow" | "review" | "block"; verdict: string; is_disposable: boolean; suggestion: string | null; relay_provider: string | null; } export interface Config { /** SeeReal key (sr_beta_...). */ apiKey?: string; /** Secret from Authentication > Hooks, format "v1,whsec_...". */ hookSecret?: string; endpoint?: string; timeoutMs?: number; blockRelays?: boolean; blockTypos?: boolean; fetch?: typeof fetch; } const json = (body: unknown, status = 200) => new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json" } }); /** Allow = empty object. Reject = { error: { http_code, message } }, which Supabase shows to the user. */ const reject = (message: string) => json({ error: { http_code: 400, message } }); export function decide(r: SeeRealResult, cfg: Config): string | null { if (r.is_disposable) return "Please use a permanent email address. Temporary inboxes are not accepted."; if (r.relay_provider && cfg.blockRelays) return "Please use your regular email address instead of a forwarding alias."; if (r.action === "block") return "This email domain cannot receive mail. Please check the address."; if (r.suggestion && cfg.blockTypos !== false) return `Did you mean ${r.suggestion}? Please check the address.`; return null; } async function check(email: string, cfg: Config): Promise { if (!cfg.apiKey) return null; try { const res = await (cfg.fetch ?? fetch)(cfg.endpoint ?? "https://app.seereal.email/api/v1/check", { method: "POST", headers: { Authorization: `Bearer ${cfg.apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ email }), signal: AbortSignal.timeout(cfg.timeoutMs ?? 3000), }); const body = await res.json().catch(() => null); return res.ok && body && typeof body.action === "string" ? (body as SeeRealResult) : null; } catch { return null; } } export async function handle(req: Request, cfg: Config): Promise { if (req.method !== "POST") return json({ error: { http_code: 405, message: "Method not allowed" } }, 405); const payload = await req.text(); // Only Supabase Auth may call this hook: verify the Standard Webhooks signature. if (!cfg.hookSecret) return json({ error: { http_code: 500, message: "Hook secret is not configured" } }, 500); let event: { user?: { email?: string } }; try { const wh = new Webhook(cfg.hookSecret.replace(/^v1,whsec_/, "")); event = wh.verify(payload, Object.fromEntries(req.headers)) as typeof event; } catch { return json({ error: { http_code: 401, message: "Invalid signature" } }, 401); } const email = event.user?.email?.trim().toLowerCase(); if (!email) return json({}); // phone or anonymous sign-up: nothing to check const result = await check(email, cfg); if (!result) { console.warn(`[seereal] check skipped for ${email.split("@")[1]}, allowing sign-up`); return json({}); // fail open } const message = decide(result, cfg); return message ? reject(message) : json({}); }