fix auth redirect bug for auth mode enabled
This commit is contained in:
parent
450fb79647
commit
459c676c02
1 changed files with 13 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
"use client";
|
"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 { useGetSettingsQuery } from "@/app/api/queries/useGetSettingsQuery";
|
||||||
import {
|
import {
|
||||||
DoclingHealthBanner,
|
DoclingHealthBanner,
|
||||||
|
|
@ -22,6 +23,7 @@ import { ChatRenderer } from "./chat-renderer";
|
||||||
|
|
||||||
export function LayoutWrapper({ children }: { children: React.ReactNode }) {
|
export function LayoutWrapper({ children }: { children: React.ReactNode }) {
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
|
const router = useRouter();
|
||||||
const { isMenuOpen } = useTask();
|
const { isMenuOpen } = useTask();
|
||||||
const { isPanelOpen } = useKnowledgeFilter();
|
const { isPanelOpen } = useKnowledgeFilter();
|
||||||
const { isLoading, isAuthenticated, isNoAuthMode } = useAuth();
|
const { isLoading, isAuthenticated, isNoAuthMode } = useAuth();
|
||||||
|
|
@ -30,6 +32,14 @@ export function LayoutWrapper({ children }: { children: React.ReactNode }) {
|
||||||
const authPaths = ["/login", "/auth/callback"];
|
const authPaths = ["/login", "/auth/callback"];
|
||||||
const isAuthPage = authPaths.includes(pathname);
|
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)
|
// Call all hooks unconditionally (React rules)
|
||||||
// But disable queries for auth pages to prevent unnecessary requests
|
// But disable queries for auth pages to prevent unnecessary requests
|
||||||
const { data: settings, isLoading: isSettingsLoading } = useGetSettingsQuery({
|
const { data: settings, isLoading: isSettingsLoading } = useGetSettingsQuery({
|
||||||
|
|
@ -49,9 +59,10 @@ export function LayoutWrapper({ children }: { children: React.ReactNode }) {
|
||||||
|
|
||||||
const isSettingsLoadingOrError = isSettingsLoading || !settings;
|
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 (
|
if (
|
||||||
isLoading ||
|
isLoading ||
|
||||||
|
(!isAuthenticated && !isNoAuthMode) ||
|
||||||
(isSettingsLoadingOrError && (isNoAuthMode || isAuthenticated))
|
(isSettingsLoadingOrError && (isNoAuthMode || isAuthenticated))
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue