Fix infinite redirect

This commit is contained in:
Arthur Belleville 2025-10-16 19:10:12 +02:00
parent 25c144afe4
commit 4bf78a0c01
No known key found for this signature in database

View file

@ -1,11 +1,11 @@
import { useEffect, useState } from "react";
import { Navigate, Outlet, useSearchParams } from "react-router-dom";
import { match } from "ts-pattern";
import { useSession } from "../contexts/SessionContext";
import { LoadingSpinner } from "./LoadingSpinner";
import { useMaybeUser } from "@ui/providers/UserStoreProvider";
export const AuthenticationGateway = () => {
const { session } = useSession();
const user = useMaybeUser();
const [isLoading, setIsLoading] = useState(true);
const [searchParams] = useSearchParams();
@ -20,24 +20,20 @@ export const AuthenticationGateway = () => {
setIsLoading(false);
}, 200);
return () => clearTimeout(timer);
}, [session]);
}, [user]);
let status: "loading" | "should-redirect" | "should-pass" = "loading";
if (isLoading) {
status = "loading";
} else if (session?.user) {
} else if (user) {
status = "should-redirect";
} else {
status = "should-pass";
}
return (
<>
{match(status)
.with("loading", () => <LoadingSpinner />)
.with("should-redirect", () => <Navigate to="/" replace />)
.with("should-pass", () => <Outlet />)
.exhaustive()}
</>
);
return match(status)
.with("loading", () => <LoadingSpinner />)
.with("should-redirect", () => <Navigate to="/" replace />)
.with("should-pass", () => <Outlet />)
.exhaustive();
};