Skip to content

Commit

Permalink
Merging 350d949 into trunk-temp/pr-2312/ebd3955b-feff-419a-9a7e-d2a2f…
Browse files Browse the repository at this point in the history
…36c0838
  • Loading branch information
trunk-io[bot] authored Nov 25, 2024
2 parents 2bfaa79 + 350d949 commit 3b2bd35
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 116 deletions.
24 changes: 10 additions & 14 deletions examples/nextjs-clerk-rate-limit/app/api/arcjet/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function GET(req: NextRequest) {
})
);

const fingerprint = ip(req);
const fingerprint = ip(req) || "127.0.0.1"; // Fall back to local IP if none

// Deduct 5 tokens from the token bucket
decision = await rl.protect(req, { fingerprint, requested: 5 });
Expand All @@ -67,19 +67,15 @@ export async function GET(req: NextRequest) {
status: 429,
}
);
} else {
// Detected a bot
return NextResponse.json(
{
error: "Forbidden",
reason: decision.reason,
},
{
status: 403,
}
);
}
}

let reset: Date | undefined;
let remaining: number | undefined;

if (decision.reason.isRateLimit()) {
reset = decision.reason.resetTime;
remaining = decision.reason.remaining;
}

return NextResponse.json({ message: "Hello World" });
}
return NextResponse.json({ message: "Hello World", reset, remaining });}
6 changes: 2 additions & 4 deletions examples/nextjs-clerk-rate-limit/app/api/token/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import { auth } from "@clerk/nextjs/server";

export async function GET(req: Request) {
const { userId, getToken } = await auth();
const { userId, getToken, redirectToSignIn } = await auth();

if (!userId) {
return new Response("Unauthorized", { status: 401 });
}
if (!userId) return redirectToSignIn()

try {
const token = await getToken();
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs-clerk-rate-limit/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextResponse } from "next/server";

const isProtectedRoute = createRouteMatcher(["/api/private", "/api/token"]);

export default clerkMiddleware((auth, request) => {
export default clerkMiddleware(async (auth, request) => {
if (isProtectedRoute(request)) {
auth.protect();
}
Expand Down
116 changes: 77 additions & 39 deletions examples/nextjs-clerk-rate-limit/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/nextjs-clerk-rate-limit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@arcjet/next": "file:../../arcjet-next",
"@clerk/nextjs": "^6.4.1",
"@clerk/nextjs": "^6.5.0",
"next": "15.0.3",
"react": "^18",
"react-dom": "^18"
Expand All @@ -26,4 +26,4 @@
"tailwindcss": "^3.4.15",
"typescript": "^5"
}
}
}
Loading

0 comments on commit 3b2bd35

Please sign in to comment.