"use client" import { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" import { Lock, CheckCircle2 } from "lucide-react" export function ChangePassword() { const [currentPassword, setCurrentPassword] = useState("") const [newPassword, setNewPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [success, setSuccess] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") setSuccess(false) setLoading(true) try { const response = await fetch("/api/user/change-password", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ currentPassword, newPassword, confirmPassword, }), }) const data = await response.json() if (!response.ok) { setError(data.error || "修改密码失败") return } setSuccess(true) setCurrentPassword("") setNewPassword("") setConfirmPassword("") // 3秒后清除成功消息 setTimeout(() => setSuccess(false), 3000) } catch (err) { setError("修改密码失败,请稍后重试") } finally { setLoading(false) } } return ( 修改密码 更改您的登录密码
{error && ( {error} )} {success && ( 密码修改成功! )}
setCurrentPassword(e.target.value)} required disabled={loading} />
setNewPassword(e.target.value)} required disabled={loading} minLength={6} />
setConfirmPassword(e.target.value)} required disabled={loading} />
) }