import { useEffect } from 'react';
import { useNavigate } from 'react-router';
import { getToken } from '@/services/auth';

/** 未登录则跳 /login */
export default function RequireAuth({ children }: { children: React.ReactNode }) {
    const navigate = useNavigate();
    const token = getToken();

    useEffect(() => {
        if (!token) navigate('/login', { replace: true });
    }, [token, navigate]);

    if (!token) return null;
    return <>{children}</>;
}
