-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
56 lines (48 loc) · 1.25 KB
/
middleware.ts
File metadata and controls
56 lines (48 loc) · 1.25 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
import { NextMiddleware, NextRequest, NextResponse } from "next/server";
import arcjet, { createMiddleware, detectBot } from "@arcjet/next";
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/server";
const aj = arcjet({
key: process.env.ARCJET_KEY!,
rules: [
detectBot({
mode: "LIVE",
allow: [
"CATEGORY:SEARCH_ENGINE",
"CATEGORY:PREVIEW",
"CATEGORY:MONITOR",
"CATEGORY:WEBHOOK",
],
}),
],
});
async function existingMiddleware(req: NextRequest) {
const anyReq = req as {
nextUrl: NextRequest["nextUrl"];
kindeAuth?: {
token?: any;
user?: any;
};
};
const url = req.nextUrl;
const orgCode =
anyReq.kindeAuth?.user?.org_code ||
anyReq.kindeAuth?.token?.org_code ||
anyReq.kindeAuth?.token?.claims?.org_code;
if (
url.pathname.startsWith("/workspace") &&
!url.pathname.includes(orgCode || "")
) {
url.pathname = `/workspace/${orgCode}`;
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export default createMiddleware(
aj,
withAuth(existingMiddleware, {
publicPaths: ["/", "/api/uploadthing"],
}) as NextMiddleware
);
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico|/rpc).*)"],
};