Files
ember-market-frontend/middleware.ts
NotII 6ab5a9ac43 hmm
2025-03-23 21:59:28 +00:00

41 lines
1.2 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
const token = req.cookies.get("Authorization")?.value;
if (!token) {
console.log("No token found, redirecting to login...");
return NextResponse.redirect(new URL("/auth/login", req.url));
}
try {
// Use our internal API route that handles the auth check server-side
// This avoids SSL issues as it's a same-origin request
const origin = req.nextUrl.origin;
const authCheckUrl = `${origin}/api/auth/check`;
console.log(`Using internal auth check URL: ${authCheckUrl}`);
const res = await fetch(authCheckUrl, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
credentials: 'include',
});
if (!res.ok) {
return NextResponse.redirect(new URL("/auth/login", req.url));
}
} catch (error) {
console.error("Authentication validation failed:", error);
return NextResponse.redirect(new URL("/auth/login", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*"],
};