-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
45 lines (36 loc) · 1.23 KB
/
Copy pathmiddleware.ts
File metadata and controls
45 lines (36 loc) · 1.23 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
// Public routes - no auth required
const isPublicRoute = createRouteMatcher([
"/",
"/ebook(.*)",
"/canvas(.*)",
]);
// Login route
const isLoginRoute = createRouteMatcher(["/login"]);
// Admin routes - require authentication
const isAdminRoute = createRouteMatcher(["/admin(.*)"]);
export default clerkMiddleware(async (auth, request) => {
// Public routes are always accessible
if (isPublicRoute(request)) {
return NextResponse.next();
}
const { userId } = await auth();
// Redirect authenticated users away from login page
if (isLoginRoute(request) && userId) {
return NextResponse.redirect(new URL("/admin", request.url));
}
// Redirect unauthenticated users from admin to login
if (isAdminRoute(request) && !userId) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
});
export const config = {
matcher: [
// Skip static files and Next.js internals
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
// Always run for API routes
"/(api|trpc)(.*)",
],
};