forked from masonfox/tome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
34 lines (28 loc) · 1.1 KB
/
Copy pathmiddleware.ts
File metadata and controls
34 lines (28 loc) · 1.1 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { middlewareAuthCheck } from "@/lib/auth";
import { withRequestContext, getLogger } from "@/lib/logger";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip static assets - anything with a file extension except .json
if (pathname.match(/\.\w+$/) && !pathname.endsWith('.json')) {
return NextResponse.next();
}
// Skip Next.js internals
if (pathname.startsWith('/_next')) {
return NextResponse.next();
}
return withRequestContext(() => {
const authResult = middlewareAuthCheck(request);
const logger = getLogger();
if (authResult) {
logger.debug({ path: request.nextUrl.pathname }, "Auth middleware intercepted request");
return authResult;
}
logger.debug({ path: request.nextUrl.pathname }, "Auth middleware passed request");
return NextResponse.next();
}, request.headers.get('x-request-id') || undefined);
}
export const config = {
matcher: '/:path*', // Match everything, exclusions handled above
};