-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (30 loc) Β· 1.16 KB
/
middleware.ts
File metadata and controls
37 lines (30 loc) Β· 1.16 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
// List of paths that should be accessible without authentication
const publicPaths = ["/sign-in", "/sign-up", "/api/", "/_next", "/favicon.ico"];
export default clerkMiddleware((auth, request) => {
// Check maintenance mode
if (process.env.NEXT_PUBLIC_MAINTENANCE_MODE === "true") {
if (request.nextUrl.pathname !== "/maintenance") {
return NextResponse.redirect(new URL("/maintenance", request.url));
}
} else if (request.nextUrl.pathname === "/maintenance") {
return NextResponse.redirect(new URL("/", request.url));
}
// Check if path is public
if (publicPaths.some((path) => request.nextUrl.pathname.startsWith(path))) {
return NextResponse.next();
}
// Allow access to home page without authentication
if (request.nextUrl.pathname === "/") {
return NextResponse.next();
}
// Check authentication for all other paths
if (!auth().userId) {
return NextResponse.redirect(new URL("/sign-up", request.url));
}
return NextResponse.next();
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};