-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
58 lines (49 loc) · 1.55 KB
/
proxy.js
File metadata and controls
58 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import arcjet, { shield, detectBot } from "@arcjet/next";
const isProtectedRoute = createRouteMatcher([
"/recipe(.*)",
"/recipes(.*)",
"/pantry(.*)",
"/dashboard(.*)",
]);
// Arcjet global protection
const aj = arcjet({
key: process.env.ARCJET_KEY,
rules: [
// Shield WAF - protects against SQL injection, XSS, etc.
shield({
mode: "LIVE", // Change to "DRY_RUN" to test without blocking
}),
// Bot detection - allow search engines, block malicious bots
detectBot({
mode: "LIVE",
allow: [
"CATEGORY:SEARCH_ENGINE", // Google, Bing, etc.
"CATEGORY:PREVIEW", // Link previews (Slack, Discord, etc.)
],
}),
],
});
export default clerkMiddleware(async (auth, req) => {
// Apply Arcjet protection FIRST (before Clerk auth check)
const decision = await aj.protect(req);
if (decision.isDenied()) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
// Then apply Clerk authentication
const { userId } = await auth();
if (!userId && isProtectedRoute(req)) {
const { redirectToSignIn } = await auth();
return redirectToSignIn();
}
return NextResponse.next();
});
export const config = {
matcher: [
// Skip Next.js internals and all static files
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Always run for API routes
"/(api|trpc)(.*)",
],
};