-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (37 loc) · 1.57 KB
/
Copy pathmiddleware.ts
File metadata and controls
41 lines (37 loc) · 1.57 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
/**
* ============================================================================
* CLERK MIDDLEWARE - CENTRALIZED AUTHENTICATION
* ============================================================================
* This middleware protects routes and handles authentication across the app.
*
* Route Protection:
* - Public routes: Landing page, sign-in, sign-up, public API endpoints
* - Protected routes: Dashboard and all sub-routes, authenticated API endpoints
*
* The middleware runs on every request and ensures:
* 1. Public routes are accessible to everyone
* 2. Protected routes redirect to sign-in if not authenticated
* 3. API routes return 401 if not authenticated (handled in route handlers)
*/
// Define public routes that don't require authentication
const isPublicRoute = createRouteMatcher([
"/", // Landing page
"/sign-in(.*)", // Sign in page and sub-routes
"/sign-up(.*)", // Sign up page and sub-routes
"/api/webhooks/(.*)", // Webhook endpoints (verified by webhook secret)
]);
export default clerkMiddleware(async (auth, request) => {
// Protect all routes except public ones
if (!isPublicRoute(request)) {
await auth.protect();
}
});
export const config = {
matcher: [
// Skip Next.js internals and all static files, unless found in search params
"/((?!_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)(.*)",
],
};