diff --git a/frontend/lib/api.js b/frontend/lib/api.js index bf244ec..3199b14 100644 --- a/frontend/lib/api.js +++ b/frontend/lib/api.js @@ -1,4 +1,4 @@ -const API_BASE = ''; // 使用相对路径,同源代理 +const API_BASE = ''; // 使用相对路径 function getAuthHeaders() { const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null; @@ -15,6 +15,12 @@ async function apiCall(url, options = {}) { }, }); + const contentType = res.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + const text = await res.text(); + throw new Error(`服务器错误: ${res.status} - ${text.substring(0, 100)}`); + } + const data = await res.json(); if (!res.ok) { diff --git a/frontend/pages/api/[...path].js b/frontend/pages/api/[...path].js new file mode 100644 index 0000000..e0510d3 --- /dev/null +++ b/frontend/pages/api/[...path].js @@ -0,0 +1,17 @@ +export default async function handler(req, res) { + const { 0: path } = req.query; + + const backendUrl = `http://localhost:3501/${path}`; + + const response = await fetch(backendUrl, { + method: req.method, + headers: { + 'Content-Type': 'application/json', + ...(req.headers.authorization ? { Authorization: req.headers.authorization } : {}), + }, + body: req.method !== 'GET' && req.method !== 'HEAD' ? JSON.stringify(req.body) : undefined, + }); + + const data = await response.json(); + res.status(response.status).json(data); +} diff --git a/frontend/pages/login.js b/frontend/pages/login.js index bc58be9..eec8182 100644 --- a/frontend/pages/login.js +++ b/frontend/pages/login.js @@ -8,7 +8,6 @@ import { useToast } from '../components/ToastProvider'; export default function Login() { const router = useRouter(); const { showToast } = useToast(); - const [isLoginMode, setIsLoginMode] = useState(true); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); @@ -26,13 +25,8 @@ export default function Login() { setLoading(true); try { - if (isLoginMode) { - await login(username, password); - showToast('登录成功', { tone: 'success' }); - } else { - await register(username, password); - showToast('注册成功', { tone: 'success' }); - } + await login(username, password); + showToast('登录成功', { tone: 'success' }); router.push('/'); } catch (err) { setError(err.message); @@ -50,31 +44,8 @@ export default function Login() {
-{isLoginMode ? '登录后继续记录每一笔收支' : '注册后即可开始管理你的账本'}
- - -登录后继续记录每一笔收支