import Head from 'next/head'; import { useRouter } from 'next/router'; import { useState, type FormEvent } from 'react'; // Keep this page independent of server-only authentication modules. export default function Login() { const router = useRouter(); const [code,setCode] = useState(''); const [error,setError] = useState(''); const [busy,setBusy] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); if (busy) return; setBusy(true); setError(''); try { const response = await fetch('/api/auth/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({code})}); const data = await response.json(); if (!response.ok) { setError(data.error || '登录没有完成'); setCode(''); return; } const next = router.query.next; window.location.assign(typeof next === 'string' && next.startsWith('/') && !next.startsWith('//') && !/[\\\r\n]/.test(next) && !next.startsWith('/login') ? next : '/'); } catch { setError('暂时无法连接,请稍后重试'); } finally { setBusy(false); } } return <>