27 lines
716 B
TypeScript
27 lines
716 B
TypeScript
import { Navigate } from 'react-router-dom';
|
|
import { useAuth } from '@/features/auth/hooks/useAuth';
|
|
import { useAuthStore } from '@/features/auth/store/authStore';
|
|
import { TokenStorage } from '@/services/tokenStorage';
|
|
import type { ReactNode } from 'react';
|
|
|
|
interface PublicRouteProps {
|
|
children: ReactNode;
|
|
}
|
|
|
|
export function PublicRoute({ children }: PublicRouteProps) {
|
|
const { isAuthenticated, user } = useAuth();
|
|
const hasToken = !!TokenStorage.getAccessToken();
|
|
const { isLoading } = useAuthStore();
|
|
|
|
if (isLoading) {
|
|
return null;
|
|
}
|
|
|
|
const isAuth = isAuthenticated || (hasToken && user);
|
|
|
|
if (isAuth) {
|
|
return <Navigate to="/dashboard" replace />;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|