Back to Integrations
Pre-Creation Hook

Supabase Auth Integration

Stop disposable inboxes, datacenter botnets, and multi-accounting before they write a single row to auth.users.

How It Works

Supabase natively supports Auth Hooks that execute synchronously whenever a user attempts to register. By attaching SiftlyAPI to the Before User Created trigger:

1. Form Submitted
User submits signup form with email, password, and client IP.
2. SiftlyAPI Guard (10-40ms)
Edge Function evaluates reputation; returns prescriptive action.
3. Deterministic Decision
block aborts database write; allow completes registration.
1

Store Your SiftlyAPI Key

Store your API key in your Supabase project secrets using the Supabase CLI or project dashboard:

Terminal
supabase secrets set SIFTLYAPI_KEY=sk_live_your_actual_api_key_here
2

Create the Edge Function Hook

Create a new Edge Function at supabase/functions/siftlyapi-signup-guard/index.ts:

supabase/functions/siftlyapi-signup-guard/index.ts
1import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
2  
3interface SupabaseAuthHookPayload {
4 user: {
5 email?: string;
6 };
7 metadata?: {
8 ip_address?: string;
9 };
10}
11  
12serve(async (req) => {
13 try {
14 const payload: SupabaseAuthHookPayload = await req.json();
15 const email = payload.user?.email;
16  
17 if (!email) {
18 // If no email is present (e.g. phone auth), allow through
19 return new Response(JSON.stringify({}), { status: 200, headers: { "Content-Type": "application/json" } });
20 }
21  
22 // Extract client IP address from Supabase metadata or forwarded headers.
23 // Fail open when neither is available — evaluating a 127.0.0.1 placeholder
24 // would be classified bogon_ip -> block, rejecting every signup.
25 const clientIP =
26 payload.metadata?.ip_address || req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "";
27  
28 if (!clientIP) {
29 console.warn("No client IP in auth hook payload, failing open");
30 return new Response(JSON.stringify({}), { status: 200, headers: { "Content-Type": "application/json" } });
31 }
32  
33 const userAgent = req.headers.get("user-agent") || "";
34  
35 // Call SiftlyAPI with 3-second fail-open timeout
36 const siftlyapiRes = await fetch("https://api.siftlyapi.com/v1/score-signup", {
37 method: "POST",
38 headers: {
39 "Content-Type": "application/json",
40 "X-API-Key": Deno.env.get("SIFTLYAPI_KEY") || "",
41 },
42 body: JSON.stringify({
43 email,
44 ip_address: clientIP,
45 user_agent: userAgent,
46 }),
47 signal: AbortSignal.timeout(3000),
48 });
49  
50 if (!siftlyapiRes.ok) {
51 // Fail-open: don't block legitimate users if API has issues
52 console.warn("SiftlyAPI returned non-200, failing open");
53 return new Response(JSON.stringify({}), { status: 200, headers: { "Content-Type": "application/json" } });
54 }
55  
56 const data = await siftlyapiRes.json();
57  
58 // Enforce prescriptive action
59 if (data.action === "block") {
60 const userMessage = data.suggested_user_message || "Signup not permitted due to security risk.";
61  
62 // Supabase Auth Hook contract: return HTTP 400 with { error: { message, http_code } }
63 return new Response(
64 JSON.stringify({
65 error: {
66 http_code: data.suggested_http_code || 400,
67 message: userMessage,
68 },
69 }),
70 {
71 status: 400,
72 headers: { "Content-Type": "application/json" },
73 }
74 );
75 }
76  
77 // action === "allow": proceed with user creation.
78 // action === "challenge": proceed, but surface the verdict so the app can
79 // gate step-up verification (e.g., require email confirmation or OTP) before
80 // granting trial credits or free-tier compute.
81 const modify =
82 data.action === "challenge"
83 ? { user_metadata: { siftlyapi_action: "challenge", requires_verification: true } }
84 : {};
85 return new Response(JSON.stringify(modify), {
86 status: 200,
87 headers: { "Content-Type": "application/json" },
88 });
89 } catch (err) {
90 // Fail-open guarantee on timeout or parser error
91 console.error("SiftlyAPI Auth Hook Error, failing open:", err);
92 return new Response(JSON.stringify({}), {
93 status: 200,
94 headers: { "Content-Type": "application/json" },
95 });
96 }
97});
3

Deploy the Function

Deploy the function to Supabase with the --no-verify-jwt flag (since Supabase Auth calls it directly):

Terminal
supabase functions deploy siftlyapi-signup-guard --no-verify-jwt
4

Attach Hook in Supabase Dashboard

  1. 1. Open your Supabase Dashboard and navigate to Authentication → Hooks.
  2. 2. Locate the Before User Created event.
  3. 3. Choose HTTPS Function (or Edge Function) and select siftlyapi-signup-guard.
  4. 4. Click Save Changes.

Testing the Integration

Test your frontend registration with both clean and disposable emails:

Frontend Test
// 1. Test clean corporate user -> Expect success
const { data, error } = await supabase.auth.signUp({
email: "founder@stripe.com",
password: "SecurePassword123!",
});
console.log(data.user); // Created successfully!
 
// 2. Test disposable email -> Expect error with suggested message
const { data: blocked, error: blockError } = await supabase.auth.signUp({
email: "bot@mailinator.com",
password: "SecurePassword123!",
});
console.log(blockError.message);
// "Temporary or disposable email addresses are not permitted. Please use your business or personal email."
Testing in Staging with Shadow Audit Mode
To verify how SiftlyAPI scores signups on your staging environment without blocking users, pass the header "X-SiftlyAPI-Mode": "audit" in the Edge Function fetch call. SiftlyAPI will calculate full threat diagnostics and return action: "allow" so no registrations are interrupted.