2023-04-08 12:50:00 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
2023-04-08 12:35:54 +00:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-04-08 12:50:00 +00:00
|
|
|
// services
|
2023-04-08 12:35:54 +00:00
|
|
|
import userService from "services/user.service";
|
2023-04-08 12:50:00 +00:00
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
|
|
|
// fetch-keys
|
|
|
|
import { CURRENT_USER } from "constants/fetch-keys";
|
2023-04-08 12:35:54 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const UserAuthorizationLayout: React.FC<Props> = ({ children }) => {
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const { data: currentUser, error } = useSWR(CURRENT_USER, () => userService.currentUser());
|
|
|
|
|
|
|
|
if (!currentUser && !error) {
|
2023-04-08 12:50:00 +00:00
|
|
|
return (
|
|
|
|
<div className="h-screen grid place-items-center p-4">
|
|
|
|
<div className="flex flex-col items-center gap-3 text-center">
|
|
|
|
<h3 className="text-xl">Loading your profile...</h3>
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2023-04-08 12:35:54 +00:00
|
|
|
}
|
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
if (error) {
|
2023-04-08 12:35:54 +00:00
|
|
|
const redirectTo = router.asPath;
|
|
|
|
|
|
|
|
router.push(`/signin?next=${redirectTo}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <>{children}</>;
|
|
|
|
};
|