fix auth redirect bug for auth mode enabled

This commit is contained in:
phact 2025-12-01 11:30:09 -05:00
parent 450fb79647
commit 459c676c02

View file

@ -1,6 +1,7 @@
"use client";
import { usePathname } from "next/navigation";
import { usePathname, useRouter } from "next/navigation";
import { useEffect } from "react";
import { useGetSettingsQuery } from "@/app/api/queries/useGetSettingsQuery";
import {
DoclingHealthBanner,
@ -22,6 +23,7 @@ import { ChatRenderer } from "./chat-renderer";
export function LayoutWrapper({ children }: { children: React.ReactNode }) {
const pathname = usePathname();
const router = useRouter();
const { isMenuOpen } = useTask();
const { isPanelOpen } = useKnowledgeFilter();
const { isLoading, isAuthenticated, isNoAuthMode } = useAuth();
@ -30,6 +32,14 @@ export function LayoutWrapper({ children }: { children: React.ReactNode }) {
const authPaths = ["/login", "/auth/callback"];
const isAuthPage = authPaths.includes(pathname);
// Redirect to login when not authenticated (and not in no-auth mode)
useEffect(() => {
if (!isLoading && !isAuthenticated && !isNoAuthMode && !isAuthPage) {
const redirectUrl = `/login?redirect=${encodeURIComponent(pathname)}`;
router.push(redirectUrl);
}
}, [isLoading, isAuthenticated, isNoAuthMode, isAuthPage, pathname, router]);
// Call all hooks unconditionally (React rules)
// But disable queries for auth pages to prevent unnecessary requests
const { data: settings, isLoading: isSettingsLoading } = useGetSettingsQuery({
@ -49,9 +59,10 @@ export function LayoutWrapper({ children }: { children: React.ReactNode }) {
const isSettingsLoadingOrError = isSettingsLoading || !settings;
// Show loading state when backend isn't ready
// Show loading state when backend isn't ready or when not authenticated (redirect will happen)
if (
isLoading ||
(!isAuthenticated && !isNoAuthMode) ||
(isSettingsLoadingOrError && (isNoAuthMode || isAuthenticated))
) {
return (