Skip to content

Commit fdcb459

Browse files
committed
1 parent b74ff97 commit fdcb459

15 files changed

+31
-299
lines changed

.env.local.example

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
# Get your Arcjet key from https://app.arcjet.com
2-
ARCJET_KEY=
3-
# Generate your Auth.js secret by running `npx auth secret`
4-
AUTH_SECRET=
5-
# Add your GitHub OAuth app credentials
6-
# See https://authjs.dev/guides/configuring-github
7-
AUTH_GITHUB_ID=
8-
AUTH_GITHUB_SECRET=
2+
ARCJET_KEY=

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ features. It is deployed at
4848
npm ci
4949
```
5050

51-
3. Rename `.env.local.example` to `.env.local` and add your Arcjet key. If you
52-
want to test the rate limiting authentication, you will also need to add an
53-
Auth.js secret and [create a GitHub OAuth
54-
app](https://authjs.dev/guides/configuring-github).
51+
3. Rename `.env.local.example` to `.env.local` and add your Arcjet key
5552

5653
4. Start the dev server
5754

@@ -69,7 +66,6 @@ server](https://arcjet.com/discord).
6966

7067
## Stack
7168

72-
- Auth: [Auth.js](https://authjs.dev/)
7369
- App: [Next.js](https://nextjs.org/)
7470
- Form handling: [React Hook Form](https://react-hook-form.com/) (see also [our
7571
full form protection

app/api/auth/[...nextauth]/route.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

app/rate-limiting/page.tsx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,13 @@ import type { Metadata } from "next";
22
import Link from "next/link";
33
import { WhatNext } from "@/components/compositions/WhatNext";
44
import { RLForm } from "@/components/RLForm";
5-
import { SignIn } from "@/components/SignIn";
6-
import { SignOut } from "@/components/SignOut";
7-
import { auth } from "@/lib/auth";
85

96
export const metadata: Metadata = {
107
title: "Rate limiting example",
118
description: "An example of Arcjet's rate limiting for Next.js.",
129
};
1310

1411
export default async function IndexPage() {
15-
const session = await auth();
16-
1712
return (
1813
<main className="page">
1914
<div className="section">
@@ -36,28 +31,7 @@ export default async function IndexPage() {
3631
<h2 className="heading-secondary">Try it</h2>
3732
<RLForm />
3833

39-
{session?.user ? (
40-
<>
41-
<p className="typography--description">
42-
You are authenticated as {session.user?.email}
43-
<span className="typography--subtitle">
44-
{" "}
45-
– the limit is set to 5 requests every 60 seconds.
46-
</span>
47-
</p>
48-
</>
49-
) : (
50-
<>
51-
<p className="typography--description">
52-
You are not authenticated
53-
<span className="typography--subtitle">
54-
{" "}
55-
– the limit is set to 2 requests every 60 seconds.
56-
</span>
57-
</p>
58-
</>
59-
)}
60-
34+
<p>The limit is set to 2 requests every 60 seconds.</p>
6135
<p className="typography--subtitle">
6236
Rate limits can be{" "}
6337
<Link
@@ -68,8 +42,6 @@ export default async function IndexPage() {
6842
</Link>{" "}
6943
e.g. to set a limit based on the authenticated user.
7044
</p>
71-
72-
{session?.user ? <SignOut /> : <SignIn />}
7345
</div>
7446

7547
<hr className="divider" />

app/rate-limiting/test/route.ts

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,31 @@
11
import { setRateLimitHeaders } from "@arcjet/decorate";
2-
import type { ArcjetDecision } from "@arcjet/next";
32
import { type NextRequest, NextResponse } from "next/server";
43
import arcjet, { fixedWindow, shield } from "@/lib/arcjet";
5-
import { auth } from "@/lib/auth";
64

75
// Opt out of caching
86
export const dynamic = "force-dynamic";
97

108
// Add rules to the base Arcjet instance outside of the handler function
11-
const aj = arcjet.withRule(
12-
// Shield detects suspicious behavior, such as SQL injection and cross-site
13-
// scripting attacks. We want to ru nit on every request
14-
shield({
15-
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
16-
}),
17-
);
18-
19-
// Define an augmented client for rate limiting users
20-
const ajForUser = aj.withRule(
21-
fixedWindow({
22-
// fingerprint requests by user ID
23-
characteristics: ["userId"],
24-
mode: "LIVE",
25-
max: 5,
26-
window: "60s",
27-
}),
28-
);
29-
30-
// Define an augmented client for rate limiting guests
31-
const ajForGuest = aj.withRule(
32-
fixedWindow({
33-
// fingerprint requests by ip address (default unless set globally)
34-
characteristics: ["ip.src"],
35-
mode: "LIVE",
36-
max: 2,
37-
window: "60s",
38-
}),
39-
);
9+
const aj = arcjet
10+
.withRule(
11+
// Shield detects suspicious behavior, such as SQL injection and cross-site
12+
// scripting attacks. We want to ru nit on every request
13+
shield({
14+
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
15+
}),
16+
)
17+
.withRule(
18+
fixedWindow({
19+
// fingerprint requests by ip address (default unless set globally)
20+
characteristics: ["ip.src"],
21+
mode: "LIVE",
22+
max: 2,
23+
window: "60s",
24+
}),
25+
);
4026

4127
export async function POST(req: NextRequest) {
42-
// Get the session
43-
const session = await auth();
44-
45-
console.log("Session: ", session);
46-
47-
let decision: ArcjetDecision;
48-
49-
// Use the user ID if the user is logged in, otherwise use the IP address
50-
if (session?.user?.id) {
51-
decision = await ajForUser.protect(req, { userId: session.user.id });
52-
} else {
53-
decision = await ajForGuest.protect(req);
54-
}
28+
const decision = await aj.protect(req);
5529

5630
console.log("Arcjet decision: ", decision);
5731

components/SignIn.tsx

Lines changed: 0 additions & 27 deletions
This file was deleted.

components/SignOut.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/auth.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)