Update middleware.ts

This commit is contained in:
NotII
2025-03-23 22:20:43 +00:00
parent e3e630c211
commit 991593c301

View File

@@ -12,12 +12,17 @@ export async function middleware(req: NextRequest) {
console.log("Middleware: Token found, validating..."); console.log("Middleware: Token found, validating...");
try { try {
// Use our internal API route that handles the auth check server-side // Get the origin but handle localhost differently to avoid SSL issues
// This avoids SSL issues as it's a same-origin request
const origin = req.nextUrl.origin; const origin = req.nextUrl.origin;
const authCheckUrl = `${origin}/api/auth/check`;
console.log(`Middleware: Using internal auth check URL: ${authCheckUrl}`); // Construct the auth check URL based on environment
// For localhost, explicitly use HTTP instead of HTTPS
const isLocalhost = origin.includes('localhost') || origin.includes('127.0.0.1');
const protocol = isLocalhost ? 'http' : 'https';
const host = req.nextUrl.host;
const authCheckUrl = `${protocol}://${host}/api/auth/check`;
console.log(`Using internal auth check URL: ${authCheckUrl}`);
const res = await fetch(authCheckUrl, { const res = await fetch(authCheckUrl, {
method: "GET", method: "GET",
@@ -38,7 +43,7 @@ export async function middleware(req: NextRequest) {
console.log("Middleware: Auth check successful, proceeding to dashboard"); console.log("Middleware: Auth check successful, proceeding to dashboard");
} catch (error) { } catch (error) {
console.error("Middleware: Authentication validation failed:", error); console.error("Authentication validation failed:", error);
return NextResponse.redirect(new URL("/auth/login", req.url)); return NextResponse.redirect(new URL("/auth/login", req.url));
} }