Files
chinese-family-tree-2/app/auth/signin/page.tsx
T
2025-12-21 18:01:06 +08:00

129 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client"
import { useState } from "react"
import { signIn, signOut } from "next-auth/react"
import { useRouter } from "next/navigation"
import Link from "next/link"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { BookOpen } from "lucide-react"
export default function SignInPage() {
const router = useRouter()
const [email, setEmail] = 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 {
// 先退出登录,清除旧的 session
await signOut({ redirect: false })
// 等待一小段时间确保 session 被清除
await new Promise(resolve => setTimeout(resolve, 500))
// 清除所有 localStorage 和 sessionStorage
localStorage.clear()
sessionStorage.clear()
// 然后重新登录
const result = await signIn("credentials", {
email,
password,
redirect: false,
})
if (result?.error) {
setError("邮箱或密码错误")
setLoading(false)
} else {
// 强制完全刷新页面,清除所有缓存
window.location.replace("/")
}
} catch (err) {
setError("登录失败,请稍后重试")
setLoading(false)
}
}
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-primary/5 via-background to-secondary/5 p-4">
<Card className="w-full max-w-md">
<CardHeader className="space-y-1 text-center">
<div className="flex justify-center mb-4">
<div className="flex h-12 w-12 items-center justify-center rounded-lg bg-primary text-primary-foreground">
<BookOpen className="h-7 w-7" />
</div>
</div>
<CardTitle className="text-2xl font-serif"></CardTitle>
<CardDescription>
</CardDescription>
</CardHeader>
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4">
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div className="space-y-2">
<Label htmlFor="email"></Label>
<Input
id="email"
type="email"
placeholder="请输入邮箱"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
disabled={loading}
autoComplete="off"
/>
</div>
<div className="space-y-2">
<Label htmlFor="password"></Label>
<Input
id="password"
type="password"
placeholder="••••••••"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
disabled={loading}
autoComplete="new-password"
/>
</div>
</CardContent>
<CardFooter className="flex flex-col space-y-4 pt-6">
<Button
type="submit"
className="w-full"
disabled={loading}
>
{loading ? "登录中..." : "登录"}
</Button>
<div className="text-sm text-center text-muted-foreground">
{" "}
<Link href="/auth/register" className="text-primary hover:underline">
</Link>
</div>
</CardFooter>
</form>
</Card>
</div>
)
}