-
Notifications
You must be signed in to change notification settings - Fork 325
/
middleware.ts
40 lines (34 loc) · 1.12 KB
/
middleware.ts
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
import { NextResponse } from "next/server";
import config from "./config";
let clerkMiddleware: (arg0: (auth: any, req: any) => any) => { (arg0: any): any; new(): any; }, createRouteMatcher;
if (config.auth.enabled) {
try {
({ clerkMiddleware, createRouteMatcher } = require("@clerk/nextjs/server"));
} catch (error) {
console.warn("Clerk modules not available. Auth will be disabled.");
config.auth.enabled = false;
}
}
const isProtectedRoute = config.auth.enabled
? createRouteMatcher(["/dashboard(.*)"])
: () => false;
export default function middleware(req: any) {
if (config.auth.enabled) {
return clerkMiddleware(async (auth, req) => {
const resolvedAuth = await auth();
if (!resolvedAuth.userId && isProtectedRoute(req)) {
return resolvedAuth.redirectToSignIn();
} else {
return NextResponse.next();
}
})(req);
} else {
return NextResponse.next();
}
}
export const middlewareConfig = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};