-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
64 lines (55 loc) · 1.76 KB
/
proxy.ts
File metadata and controls
64 lines (55 loc) · 1.76 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.ts
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
const isPublicRoute = createRouteMatcher([
"/",
"/sign-in(.*)",
"/sign-up(.*)",
"/marketplace(.*)",
"/post-auth(.*)",
]);
export default clerkMiddleware(async (auth, req) => {
const { userId } = await auth();
const { pathname } = req.nextUrl;
////////////////////////////////////////
// 1. BYPASS STATIC FILES
////////////////////////////////////////
if (
pathname.startsWith("/_next") ||
pathname.includes(".")
) {
return NextResponse.next();
}
////////////////////////////////////////
// 2. API ROUTES - Let route handlers handle auth
////////////////////////////////////////
if (pathname.startsWith("/api")) {
// API routes use requireAdmin() internally
return NextResponse.next();
}
////////////////////////////////////////
// 3. NOT SIGNED IN
////////////////////////////////////////
if (!userId) {
if (!isPublicRoute(req)) {
const signInUrl = new URL("/sign-in", req.url);
signInUrl.searchParams.set("redirect_url", pathname);
return NextResponse.redirect(signInUrl);
}
return NextResponse.next();
}
////////////////////////////////////////
// 4. PREVENT AUTH PAGES WHEN SIGNED IN
////////////////////////////////////////
if (pathname.startsWith("/sign-in") || pathname.startsWith("/sign-up")) {
return NextResponse.redirect(new URL("/post-auth", req.url));
}
////////////////////////////////////////
// 5. ALL OTHER ROUTES - Allow
// (Admin layout handles role check)
////////////////////////////////////////
return NextResponse.next();
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};