re-enable middleware

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso 2024-10-03 08:41:50 -04:00
parent 4acfeb0a93
commit 39de5ee75f
4 changed files with 51 additions and 49 deletions

42
middleware.ts Normal file
View File

@ -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();
// }
}

View File

@ -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();
}
}

16
package-lock.json generated
View File

@ -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"
},

View File

@ -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",