refactor: Enhance token handling and debug logging in proxy function

- Explicitly specified the cookie name for token retrieval to align with NextAuth configuration.
- Improved debug logging to include cookie presence checks and detailed cookie information for better troubleshooting.
- Updated comments for clarity on the changes made to token handling and logging.
This commit is contained in:
ilia 2026-01-04 13:30:27 -05:00
parent b060459f60
commit 76cd2782ec

View File

@ -11,17 +11,25 @@ export async function proxy(request: NextRequest) {
} }
// Get token (works in Edge runtime) // Get token (works in Edge runtime)
// getToken automatically detects the cookie name from NextAuth config // Explicitly specify the cookie name to match NextAuth config
const cookieName = "__Secure-authjs.session-token"
const token = await getToken({ const token = await getToken({
req: request, req: request,
secret: process.env.NEXTAUTH_SECRET secret: process.env.NEXTAUTH_SECRET,
cookieName: cookieName
}) })
// Debug logging for production troubleshooting // Debug logging for production troubleshooting
const cookieHeader = request.headers.get("cookie") || ""
const hasCookie = cookieHeader.includes(cookieName)
if (!token) { if (!token) {
console.log("Middleware: No token found", { console.log("Middleware: No token found", {
pathname, pathname,
cookieHeader: request.headers.get("cookie")?.substring(0, 200), cookieName,
hasCookie,
cookieHeader: cookieHeader.substring(0, 300),
allCookies: cookieHeader.split(";").map(c => c.trim().substring(0, 50)),
origin: request.headers.get("origin"), origin: request.headers.get("origin"),
referer: request.headers.get("referer") referer: request.headers.get("referer")
}) })