-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathproxy.ts
More file actions
71 lines (61 loc) · 2.14 KB
/
Copy pathproxy.ts
File metadata and controls
71 lines (61 loc) · 2.14 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
65
66
67
68
69
70
71
import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
import { getSessionCookie } from "better-auth/cookies";
import { NextRequest, NextResponse } from "next/server";
const intlMiddleware = createMiddleware(routing);
// Admin-only API paths — cookie presence checked here, role checked server-side
const ADMIN_ONLY_PATHS = [
"/api/user/activateAdmin",
"/api/user/deactivateAdmin",
"/api/user/activate",
"/api/user/deactivate",
"/api/user/inviteuser",
"/api/admin",
];
export async function proxy(req: NextRequest) {
const path = req.nextUrl.pathname;
// Inngest webhook — pass through, Inngest handles its own auth via signing key
if (path.startsWith("/api/inngest")) {
return NextResponse.next();
}
// better-auth API routes — pass through to better-auth handler
if (path.startsWith("/api/auth")) {
return NextResponse.next();
}
const sessionCookie = getSessionCookie(req);
// Admin-only routes — require session cookie (role checked server-side)
if (ADMIN_ONLY_PATHS.some((p) => path.startsWith(p))) {
if (!sessionCookie) {
return NextResponse.json({ error: "Unauthenticated" }, { status: 401 });
}
return NextResponse.next();
}
// Non-API routes — redirect to sign-in if no session cookie
if (!path.startsWith("/api")) {
if (!sessionCookie) {
// Allow auth pages (sign-in, register, pending, inactive)
const authPaths = ["/sign-in", "/register", "/pending", "/inactive"];
const isAuthPage = authPaths.some((p) => path.includes(p));
if (!isAuthPage) {
return NextResponse.redirect(new URL("/sign-in", req.nextUrl));
}
}
}
// Non-API routes — delegate to next-intl
return intlMiddleware(req);
}
export const config = {
matcher: [
// Admin-only API paths
"/api/user/activateAdmin/:path*",
"/api/user/deactivateAdmin/:path*",
"/api/user/activate/:path*",
"/api/user/deactivate/:path*",
"/api/user/inviteuser",
"/api/admin/:path*",
// better-auth API
"/api/auth/:path*",
// All non-API routes (existing intl matcher)
"/((?!api|trpc|_next|_vercel|.*\\..*).*)",
],
};