Files
freedakgmail e92884ae53 0.0.1.0
2025-11-23 03:07:39 +08:00

132 lines
3.9 KiB
TypeScript
Raw Permalink 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 { 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 (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Lock className="h-5 w-5" />
</CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
{error && (
<Alert variant="destructive">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
{success && (
<Alert className="bg-green-50 border-green-200">
<CheckCircle2 className="h-4 w-4 text-green-600" />
<AlertDescription className="text-green-800">
</AlertDescription>
</Alert>
)}
<div className="space-y-2">
<Label htmlFor="currentPassword"></Label>
<Input
id="currentPassword"
type="password"
placeholder="输入当前密码"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
required
disabled={loading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="newPassword"></Label>
<Input
id="newPassword"
type="password"
placeholder="至少6个字符"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
required
disabled={loading}
minLength={6}
/>
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword"></Label>
<Input
id="confirmPassword"
type="password"
placeholder="再次输入新密码"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
disabled={loading}
/>
</div>
<Button type="submit" disabled={loading} className="w-full">
{loading ? "修改中..." : "修改密码"}
</Button>
</form>
</CardContent>
</Card>
)
}