Fix infinite redirect
This commit is contained in:
parent
25c144afe4
commit
4bf78a0c01
1 changed files with 9 additions and 13 deletions
|
|
@ -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();
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue