-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
61 lines (52 loc) · 1.39 KB
/
middleware.ts
File metadata and controls
61 lines (52 loc) · 1.39 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
/* eslint-disable @typescript-eslint/no-explicit-any */
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',
],
}),
],
});
const existingMiddleware = async (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 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|/rpc).*)'],
runtime: 'nodejs',
};