From b51606eb778aefb42cd3c8e5dc6805cc31ad0e5e Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Thu, 27 Mar 2025 09:28:52 +0100 Subject: [PATCH] Improve redirections --- ui/src/components/ProtectedRoute.tsx | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ui/src/components/ProtectedRoute.tsx b/ui/src/components/ProtectedRoute.tsx index bb7320a..ffd179e 100644 --- a/ui/src/components/ProtectedRoute.tsx +++ b/ui/src/components/ProtectedRoute.tsx @@ -1,6 +1,6 @@ -import { ReactNode } from "react"; +import { ReactNode, useEffect } from "react"; import { useSession } from "../contexts/SessionContext"; -import { Navigate } from "react-router-dom"; +import { useNavigate } from "react-router-dom"; interface ProtectedRouteProps { children: ReactNode; to?: string; @@ -8,9 +8,16 @@ interface ProtectedRouteProps { export const ProtectedRoute = ({ children, to }: ProtectedRouteProps) => { const { session } = useSession(); - if (!session) { - return ; - } + const navigate = useNavigate(); + + useEffect(() => { + let timer = setTimeout(() => { + if (!session) { + navigate(to ?? "/landing"); + } + }, 300); + return () => clearTimeout(timer); + }, [session, navigate, to]); // If authenticated, render the protected component return <>{children};