"use client"; import { useState, useEffect } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; export default function OwnerPage() { const router = useRouter(); const [isLogin, setIsLogin] = useState(true); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const [user, setUser] = useState<{ username: string } | null>(null); useEffect(() => { checkAuth(); }, []); async function checkAuth() { try { const res = await fetch("/api/auth/me"); const data = await res.json(); if (data.user) { setUser(data.user); router.push("/owner/dashboard"); } } catch (error) { console.error("检查登录状态失败", error); } } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(""); setLoading(true); try { const endpoint = isLogin ? "/api/auth/login" : "/api/auth/register"; const res = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); const data = await res.json(); if (!res.ok) { setError(data.error || "操作失败"); return; } router.push("/owner/dashboard"); } catch (error) { setError("网络错误,请重试"); } finally { setLoading(false); } } return (

房东入口

🏗️

{isLogin ? "房东登录" : "房东注册"}

{isLogin ? "登录后管理您的房源" : "注册后发布和管理房源"}

{error && (
{error}
)}
setUsername(e.target.value)} placeholder="请输入用户名(至少3位)" className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-orange-500 focus:ring-2 focus:ring-orange-200 outline-none transition" required minLength={3} />
setPassword(e.target.value)} placeholder="请输入密码(至少6位)" className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-orange-500 focus:ring-2 focus:ring-orange-200 outline-none transition" required minLength={6} />
{isLogin ? (

还没有账号?{" "}

) : (

已有账号?{" "}

)}

← 返回找房

); }