{isLogin ? "房东登录" : "房东注册"}
{isLogin ? "登录后管理您的房源" : "注册后发布和管理房源"}
← 返回找房
"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 ? "登录后管理您的房源" : "注册后发布和管理房源"}
← 返回找房