-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
52 lines (46 loc) · 1.4 KB
/
middleware.ts
File metadata and controls
52 lines (46 loc) · 1.4 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
// Define matchers for protected, public, and ignored routes
const isProtectedRoute = createRouteMatcher([
"/ask-question(.*)",
"/question/:id",
"/collection",
]);
const isPublicRoute = createRouteMatcher([
"/",
"/api/webhook",
"/question/edit/:id",
"/tags",
"/tags/:id",
"/profile/:id",
"/profile/edit",
"/community",
"/jobs",
"/sign-up(.*)",
"/sign-in(.*)",
]);
// Add ignored routes here
const isIgnoredRoute = createRouteMatcher(["/api/webhook", "/api/chatgpt"]);
export default clerkMiddleware(async (auth, req) => {
const url = req.nextUrl.pathname; // Access the pathname instead of the full URL
// Check if the route is ignored
if (isIgnoredRoute(req)) {
return;
}
if (isProtectedRoute(req)) {
// Protect only protected routes
await auth().protect(); // Ensure to await the authentication check
} else if (isPublicRoute(req)) {
// eslint-disable-next-line no-useless-return
return;
} else {
console.error(`Route ${url} does not match any defined route types.`);
}
});
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)(.*)",
],
};