fix(auth): remove middleware self-fetch and SSL loop

This commit is contained in:
Schubert Ferenc 2026-07-12 11:01:19 +02:00
parent 9c6b184e39
commit 4f669a3426
345 changed files with 723 additions and 705 deletions

View file

@ -14,18 +14,7 @@ const protectedPrefixes = [
"/profile"
];
async function loadCurrentUser(request: NextRequest) {
const response = await fetch(new URL("/api/me", request.url), {
headers: { cookie: request.headers.get("cookie") ?? "" },
cache: "no-store"
});
if (!response.ok) {
return null;
}
return (await response.json()) as { must_change_password?: boolean };
}
export async function middleware(request: NextRequest) {
export function middleware(request: NextRequest) {
const token = request.cookies.get(AUTH_COOKIE_NAME)?.value;
const isProtected = protectedPrefixes.some(
(prefix) => request.nextUrl.pathname === prefix || request.nextUrl.pathname.startsWith(`${prefix}/`)
@ -38,18 +27,10 @@ export async function middleware(request: NextRequest) {
return NextResponse.next();
}
const currentUser = await loadCurrentUser(request);
const requiresPasswordChange = Boolean(currentUser?.must_change_password);
const isLogin = request.nextUrl.pathname === "/login";
const isPasswordProfile = request.nextUrl.pathname === "/profile/security";
if (isLogin) {
const target = requiresPasswordChange ? "/profile/security" : "/dashboard";
return NextResponse.redirect(new URL(target, request.url));
}
if (requiresPasswordChange && isProtected && !isPasswordProfile) {
return NextResponse.redirect(new URL("/profile/security", request.url));
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();