-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
64 lines (53 loc) · 2.06 KB
/
middleware.ts
File metadata and controls
64 lines (53 loc) · 2.06 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
57
58
59
60
61
62
63
64
// middleware so that after login user gets redirected to corrected route
/* eslint-disable */
import arcjet, { createMiddleware, detectBot } from "@arcjet/next";
import { withAuth } from "@kinde-oss/kinde-auth-nextjs/server";
import { NextMiddleware, NextRequest, NextResponse } from "next/server";
const aj = arcjet({
key: process.env.ARCJET_KEY!,
rules: [
detectBot({
mode: "LIVE",
allow: [
'CATEGORY:SEARCH_ENGINE',
"CATEGORY:PREVIEW",
'CATEGORY:MONITOR',
"CATEGORY:WEBHOOK",
],
})
]
})
//COMMENTED THIS BCZ IT DONT HAVE AUTH SOSEE BELOW MIDDLEWARE WITH AUTH INCLUDED!
// async function existingMiddlewares(req: NextRequest) {
// const { getClaim } = getKindeServerSession();
// const orgCode = await getClaim("org_code");
// const url = req.nextUrl;
// if (url.pathname.startsWith('/workspace') && !url.pathname.includes(orgCode?.value || "")) {
// url.pathname = `/workspace/${orgCode?.value}`;
// return NextResponse.redirect(url)
// }
// return NextResponse.next();
// }
async function existingMiddlewares(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(existingMiddlewares, {
publicPaths: ["/", "/api/uploadthing"],
}) as NextMiddleware
);
export const config = {
//matcher tells Next.js which route to run the middleware on.
//This runs the middleware on all routes except for static assets.
matcher: ["/((?!_next/static|_next/image|favicon.ico|/rpc).*)"],
runtime: "nodejs",
}