Improve redirections

This commit is contained in:
Arthur Belleville 2025-03-27 09:28:52 +01:00
parent c6ff823601
commit b51606eb77
No known key found for this signature in database

View file

@ -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 <Navigate to={to ?? "/login"} replace />;
}
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}</>;