refactor: clarify frontend module ownership
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type { FC, PropsWithChildren } from "react";
|
||||
|
||||
import { type FC, type PropsWithChildren, useState } from "react";
|
||||
import { Navigate, useLocation } from "react-router";
|
||||
|
||||
import { useAuth } from "@/features/auth";
|
||||
@@ -8,16 +7,22 @@ import { RouteSkeleton } from "./RouteFallback";
|
||||
|
||||
const Protected: FC<PropsWithChildren> = (props) => {
|
||||
const { children } = props;
|
||||
const { ready, user } = useAuth();
|
||||
const { ready, signedOut, user } = useAuth();
|
||||
const location = useLocation();
|
||||
const [wasAuthenticated, setWasAuthenticated] = useState(false);
|
||||
if (user && !wasAuthenticated) setWasAuthenticated(true);
|
||||
if (!ready) return <RouteSkeleton variant="shell" />;
|
||||
|
||||
const loginState = signedOut && wasAuthenticated
|
||||
? undefined
|
||||
: { returnTo: `${location.pathname}${location.search}` };
|
||||
|
||||
return user
|
||||
? <>{children}</>
|
||||
: (
|
||||
<Navigate
|
||||
replace
|
||||
state={{ returnTo: `${location.pathname}${location.search}` }}
|
||||
state={loginState}
|
||||
to="/login"
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user