plane/web/layouts/auth-layout/user-wrapper.tsx

40 lines
992 B
TypeScript
Raw Normal View History

2023-10-02 19:03:03 +00:00
import { FC, ReactNode } from "react";
import { useRouter } from "next/router";
import useSWR from "swr";
// services
import userService from "services/user.service";
// ui
import { Spinner } from "components/ui";
// fetch-keys
import { CURRENT_USER } from "constants/fetch-keys";
2023-10-02 19:03:03 +00:00
export interface IUserAuthWrapper {
children: ReactNode;
}
2023-10-02 19:03:03 +00:00
export const UserAuthWrapper: FC<IUserAuthWrapper> = (props) => {
const { children } = props;
// router
const router = useRouter();
2023-10-02 19:03:03 +00:00
// fetching user information
const { data: currentUser, error } = useSWR(CURRENT_USER, () => userService.currentUser());
if (!currentUser && !error) {
return (
<div className="h-screen grid place-items-center p-4">
<div className="flex flex-col items-center gap-3 text-center">
<Spinner />
</div>
</div>
);
}
if (error) {
const redirectTo = router.asPath;
router.push(`/?next=${redirectTo}`);
return null;
}
return <>{children}</>;
};