"use client"; import { useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import Link from "next/link"; import { MessagesSquare } from "lucide-react"; import { apiLogin } from "@/lib/api"; export default function LoginPage() { const router = useRouter(); const searchParams = useSearchParams(); const redirect = searchParams.get("redirect") || "/"; const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); setLoading(true); try { const res = await apiLogin(username, password); // 登录成功后后端通过 Set-Cookie 写入 HttpOnly JWT,前端无需存储 token if (res.user) { router.push(redirect); } else { setError(res.error || "登录失败"); } } finally { setLoading(false); } }; return (
登录后发帖、评论与点赞
还没有账号? 立即注册