132 lines
3.9 KiB
TypeScript
132 lines
3.9 KiB
TypeScript
"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>
|
||
)
|
||
}
|