'use client' import { useState } from 'react' export function LoginForm() { const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(''); setLoading(true) try { const res = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username, password }) }) const data = await res.json() if (!res.ok) { setError(data.error || '登录失败'); return } // 直接从 URL 读取 redirect 参数,避免 Suspense/闭包导致的值捕获问题 const params = new URLSearchParams(window.location.search) const redirect = params.get('redirect') const dest = (redirect && redirect.startsWith('/')) ? redirect : '/dashboard' window.location.href = dest } catch { setError('网络错误,请重试') } finally { setLoading(false) } } return (