Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
71 lines (61 loc) · 2.03 KB
/
Copy pathmiddleware.ts
File metadata and controls
71 lines (61 loc) · 2.03 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { auth } from "@/lib/auth/auth"
export default auth((req) => {
// Monitor header sizes to debug 431 errors in development
if (process.env.NODE_ENV === 'development') {
const headerSize = JSON.stringify(Object.fromEntries(req.headers)).length;
const cookieSize = req.headers.get('cookie')?.length || 0;
if (headerSize > 16000 || cookieSize > 8000) {
console.warn(`⚠️ Large request headers detected:
- Total header size: ${headerSize} bytes
- Cookie size: ${cookieSize} bytes
- URL: ${req.nextUrl.pathname}
- User-Agent: ${req.headers.get('user-agent')?.substring(0, 100)}...`);
}
}
const isAuthPage = req.nextUrl.pathname.startsWith("/auth");
const isApiAuthRoute = req.nextUrl.pathname.startsWith("/api/auth");
const isPublicRoute = [
"/",
"/auth/signin",
"/auth/signout",
"/auth/error",
"/auth/verify-request",
].includes(req.nextUrl.pathname);
// Allow API auth routes
if (isApiAuthRoute) {
return undefined;
}
// Allow public routes
if (isPublicRoute) {
return undefined;
}
const isLoggedIn = !!req.auth;
// Redirect authenticated users away from auth pages to dashboard
if (isAuthPage && isLoggedIn) {
return Response.redirect(new URL("/", req.nextUrl.origin));
}
// Redirect unauthenticated users to signin for protected routes
if (!isLoggedIn && !isAuthPage) {
const callbackUrl = req.nextUrl.pathname + req.nextUrl.search;
return Response.redirect(
new URL(
`/auth/signin?callbackUrl=${encodeURIComponent(callbackUrl)}`,
req.nextUrl.origin
)
);
}
// Allow the request to proceed
return undefined;
})
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public files (public folder)
*/
"/((?!_next/static|_next/image|favicon.svg|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
}