Back to Integrations
Server-Side Defense

Next.js 16 & Auth.js Integration

Evaluate signup reputation synchronously in 10-40ms before committing user accounts to your database in Server Actions or Route Handlers.

1

Create the Reusable SiftlyAPI Helper

Create a lightweight utility helper at lib/siftlyapi.ts that automatically extracts client IP and User-Agent from incoming Next.js request headers:

lib/siftlyapi.ts
1import { headers } from "next/headers";
2  
3export interface SiftlyAPIScoreResponse {
4 action: "allow" | "challenge" | "block";
5 audit_action?: "allow" | "challenge" | "block";
6 suggested_user_message?: string;
7 suggested_http_code?: number;
8 trust_score: number;
9 risk_level: "low" | "medium" | "high";
10 reasons: Array<{
11 factor: string;
12 score: number;
13 reason_code: string;
14 description: string;
15 risk_level: "low" | "medium" | "high";
16 }>;
17 request_id: string;
18}
19  
20export async function verifySignup(email: string): Promise<SiftlyAPIScoreResponse> {
21 const headerList = await headers();
22 const forwardedFor = headerList.get("x-forwarded-for");
23 const realIp = headerList.get("x-real-ip");
24 const ip = forwardedFor?.split(",")[0]?.trim() || realIp || "";
25 const userAgent = headerList.get("user-agent") || "";
26  
27 // No routeable client IP available (e.g. direct loopback listener): skip scoring and
28 // fail open. Evaluating a 127.0.0.1 placeholder would be classified bogon_ip -> block.
29 if (!ip) {
30 console.warn("No client IP available, skipping SiftlyAPI evaluation");
31 return {
32 action: "allow",
33 trust_score: 85,
34 risk_level: "low",
35 reasons: [],
36 request_id: "no_client_ip_skip",
37 };
38 }
39  
40 try {
41 const res = await fetch("https://api.siftlyapi.com/v1/score-signup", {
42 method: "POST",
43 headers: {
44 "Content-Type": "application/json",
45 "X-API-Key": process.env.SIFTLYAPI_KEY || "",
46 },
47 body: JSON.stringify({
48 email,
49 ip_address: ip,
50 user_agent: userAgent,
51 }),
52 signal: AbortSignal.timeout(3000),
53 });
54  
55 if (!res.ok) {
56 // Fail-open guarantee on upstream issues
57 console.warn("SiftlyAPI returned non-200, failing open");
58 return {
59 action: "allow",
60 trust_score: 85,
61 risk_level: "low",
62 reasons: [],
63 request_id: "fallback",
64 };
65 }
66  
67 return await res.json();
68 } catch (error) {
69 // Fail-open guarantee on network timeout
70 console.error("SiftlyAPI verifySignup timeout/error, failing open:", error);
71 return {
72 action: "allow",
73 trust_score: 85,
74 risk_level: "low",
75 reasons: [],
76 request_id: "fallback_error",
77 };
78 }
79}
2

Recipe A: Next.js Server Action

Use Server Actions for form submissions. If SiftlyAPI returns action: "block", return the suggested error message to render in the UI without inserting into your database:

actions/signup.ts
1"use server";
2  
3import { verifySignup } from "@/lib/siftlyapi";
4  
5export async function handleSignup(formData: FormData) {
6 const email = formData.get("email") as string;
7 const password = formData.get("password") as string;
8  
9 if (!email || !password) {
10 return { success: false, error: "Email and password are required." };
11 }
12  
13 // 1. Guard against fraud in 10-40ms
14 const decision = await verifySignup(email);
15  
16 if (decision.action === "block") {
17 return {
18 success: false,
19 error: decision.suggested_user_message || "Registration from this email provider is not allowed.",
20 };
21 }
22  
23 // 2. If challenged, prompt for step-up verification
24 if (decision.action === "challenge") {
25 // Send magic link or trigger Cloudflare Turnstile
26 return { success: true, requiresVerification: true };
27 }
28  
29 // 3. Clean user -> proceed with database creation
30 // await db.users.create({ data: { email, passwordHash } });
31  
32 return { success: true };
33}
3

Recipe B: Route Handler (API Endpoint)

If your frontend registers accounts via standard REST requests:

app/api/auth/register/route.ts
1import { NextResponse } from "next/server";
2import { verifySignup } from "@/lib/siftlyapi";
3  
4export async function POST(req: Request) {
5 const { email, password } = await req.json();
6  
7 const decision = await verifySignup(email);
8  
9 if (decision.action === "block") {
10 return NextResponse.json(
11 {
12 error: decision.suggested_user_message || "Signup blocked due to security policy.",
13 reason: decision.reasons?.[0]?.reason_code,
14 },
15 { status: decision.suggested_http_code || 400 }
16 );
17 }
18  
19 // Create user in database...
20 return NextResponse.json({ success: true }, { status: 201 });
21}
4

Recipe C: NextAuth / Auth.js signIn Callback

If you are using NextAuth or Auth.js, intercept registrations in the signIn callback:

auth.config.ts
1import type { NextAuthConfig } from "next-auth";
2import { verifySignup } from "@/lib/siftlyapi";
3  
4export const authConfig = {
5 providers: [/* your providers */],
6 callbacks: {
7 async signIn({ user, account, profile }) {
8 if (!user.email) return true;
9  
10 // Check signup reputation
11 const decision = await verifySignup(user.email);
12  
13 if (decision.action === "block") {
14 console.warn(`Blocked NextAuth signup for ${user.email}: ${decision.reasons?.[0]?.reason_code}`);
15 // Returning false aborts signin with AccessDenied
16 return false;
17 }
18  
19 return true;
20 },
21 },
22} satisfies NextAuthConfig;
Performance Advantage
Because SiftlyAPI delivers 10-40ms global edge scoring, calling verifySignup adds only 10-40ms of overhead to your Server Actions, completely imperceptible to real human registrants while halting automated bot attacks.