| import { useEffect } from 'react';
|
| import { useRouter } from '@tanstack/react-router';
|
| import { isAuthError } from '@/gql/graphql';
|
| import { useAuthStore } from '@/stores/authStore';
|
| import { Skeleton } from '@/components/ui/skeleton';
|
| import { useMe } from '@/features/auth/data/auth';
|
|
|
| interface AuthGuardProps {
|
| children: React.ReactNode;
|
| }
|
|
|
| export function AuthGuard({ children }: AuthGuardProps) {
|
| const router = useRouter();
|
| const { accessToken } = useAuthStore((state) => state.auth);
|
|
|
|
|
| const { isLoading: isMeLoading, error: meError } = useMe();
|
|
|
| useEffect(() => {
|
|
|
| if (!accessToken) {
|
| const currentPath = window.location.pathname;
|
|
|
| if (
|
| !currentPath.startsWith('/sign-in') &&
|
| !currentPath.startsWith('/sign-up') &&
|
| !currentPath.startsWith('/initialization') &&
|
| !currentPath.startsWith('/forgot-password') &&
|
| !currentPath.startsWith('/otp')
|
| ) {
|
| router.navigate({ to: '/sign-in' });
|
| }
|
| }
|
| }, [accessToken, router]);
|
|
|
|
|
| useEffect(() => {
|
| if (meError && accessToken && isAuthError(meError)) {
|
|
|
| router.navigate({ to: '/sign-in' });
|
| }
|
| }, [meError, accessToken, router]);
|
|
|
|
|
| if (!accessToken) {
|
| const currentPath = window.location.pathname;
|
|
|
| if (
|
| currentPath.startsWith('/sign-in') ||
|
| currentPath.startsWith('/sign-up') ||
|
| currentPath.startsWith('/initialization') ||
|
| currentPath.startsWith('/forgot-password') ||
|
| currentPath.startsWith('/otp')
|
| ) {
|
| return <>{children}</>;
|
| }
|
|
|
| return (
|
| <div className='flex h-screen items-center justify-center'>
|
| <div className='space-y-4'>
|
| <Skeleton className='h-8 w-48' />
|
| <Skeleton className='h-4 w-32' />
|
| </div>
|
| </div>
|
| );
|
| }
|
|
|
|
|
| if (accessToken && isMeLoading) {
|
| return (
|
| <div className='flex h-screen items-center justify-center'>
|
| <div className='space-y-4'>
|
| <Skeleton className='h-8 w-48' />
|
| <Skeleton className='h-4 w-32' />
|
| </div>
|
| </div>
|
| );
|
| }
|
|
|
| return <>{children}</>;
|
| }
|
|
|