From 39de5ee75f06f6eb034c127f6382692a44e8ddef Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Thu, 3 Oct 2024 08:41:50 -0400 Subject: [PATCH] re-enable middleware Signed-off-by: Xe Iaso --- middleware.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ middleware2.ts | 40 ---------------------------------------- package-lock.json | 16 ++++++++-------- package.json | 2 +- 4 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 middleware.ts delete mode 100644 middleware2.ts diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..25c97b8 --- /dev/null +++ b/middleware.ts @@ -0,0 +1,42 @@ +import arcjet, { detectBot } from "@arcjet/next"; +import { NextRequest, NextResponse } from "next/server"; + +export const config = { + // matcher tells Next.js which routes to run the middleware on. + // This runs the middleware on all routes except for static assets. + matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"], +}; + +const aj = arcjet({ + key: process.env.ARCJET_KEY!, + rules: [ + detectBot({ + mode: "LIVE", + allow: [ + "CATEGORY:SEARCH_ENGINE", + "CATEGORY:PREVIEW", + "CATEGORY:SOCIAL", + ], + }), + ], +}); + +export default async function middleware(request: NextRequest) { + console.log(request.headers); + + // const decision = await aj.protect(request); + // + // if ( + // // If this deny comes from a bot rule then block the request. You can + // // customize this logic to fit your needs e.g. changing the status code. + // decision.isDenied() && + // decision.reason.isBot() + // ) { + // return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); + // } else if (decision.isErrored()) { + // console.warn("Arcjet error", decision.reason.message); + // return NextResponse.json({ error: "Bad request" }, { status: 400 }); + // } else { + // return NextResponse.next(); + // } +} \ No newline at end of file diff --git a/middleware2.ts b/middleware2.ts deleted file mode 100644 index eb593dd..0000000 --- a/middleware2.ts +++ /dev/null @@ -1,40 +0,0 @@ -import arcjet, { detectBot } from "@arcjet/next"; -import { NextRequest, NextResponse } from "next/server"; - -export const config = { - // matcher tells Next.js which routes to run the middleware on. - // This runs the middleware on all routes except for static assets. - matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"], -}; - -const aj = arcjet({ - key: process.env.ARCJET_KEY!, - rules: [ - detectBot({ - mode: "LIVE", - allow: [ - "CATEGORY:SEARCH_ENGINE", - "CATEGORY:PREVIEW", - "CATEGORY:SOCIAL", - ], - }), - ], -}); - -export default async function middleware(request: NextRequest) { - const decision = await aj.protect(request); - - if ( - // If this deny comes from a bot rule then block the request. You can - // customize this logic to fit your needs e.g. changing the status code. - decision.isDenied() && - decision.reason.isBot() - ) { - return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); - } else if (decision.isErrored()) { - console.warn("Arcjet error", decision.reason.message); - return NextResponse.json({ error: "Bad request" }, { status: 400 }); - } else { - return NextResponse.next(); - } -} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 97d430f..bebb0e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,7 +27,7 @@ "@testing-library/dom": "^10.4.0", "@testing-library/jest-dom": "^6.5.0", "@testing-library/react": "^16.0.1", - "@types/node": "^20", + "@types/node": "^20.16.10", "@types/react": "^18", "@types/react-dom": "^18", "concurrently": "^9.0.1", @@ -3672,13 +3672,13 @@ "license": "MIT" }, "node_modules/@types/node": { - "version": "20.14.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.9.tgz", - "integrity": "sha512-06OCtnTXtWOZBJlRApleWndH4JsRVs1pDCc8dLSQp+7PpUpX3ePdHyeNSFTeSe7FtKyQkrlPvHwJOW3SLd8Oyg==", + "version": "20.16.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.16.10.tgz", + "integrity": "sha512-vQUKgWTjEIRFCvK6CyriPH3MZYiYlNy0fKiEYHWbcoWLEgs4opurGGKlebrTLqdSMIbXImH6XExNiIyNUv3WpA==", "dev": true, "license": "MIT", "dependencies": { - "undici-types": "~5.26.4" + "undici-types": "~6.19.2" } }, "node_modules/@types/prop-types": { @@ -13438,9 +13438,9 @@ } }, "node_modules/undici-types": { - "version": "5.26.5", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", "dev": true, "license": "MIT" }, diff --git a/package.json b/package.json index e9e36e2..3fb1de7 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@testing-library/dom": "^10.4.0", "@testing-library/jest-dom": "^6.5.0", "@testing-library/react": "^16.0.1", - "@types/node": "^20", + "@types/node": "^20.16.10", "@types/react": "^18", "@types/react-dom": "^18", "concurrently": "^9.0.1",