import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card.tsx"; import { Key } from "lucide-react"; import React, { useState } from "react"; import { changePassword } from "@/ui/main-axios.ts"; import { Label } from "@/components/ui/label.tsx"; import { PasswordInput } from "@/components/ui/password-input.tsx"; import { Button } from "@/components/ui/button.tsx"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert.tsx"; import { toast } from "sonner"; import { useTranslation } from "react-i18next"; interface PasswordResetProps { userInfo: { username: string; is_admin: boolean; is_oidc: boolean; totp_enabled: boolean; }; } export function PasswordReset({ userInfo }: PasswordResetProps) { const [error, setError] = useState(null); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [loading, setLoading] = useState(false); const { t } = useTranslation(); async function handleChangePassword() { setError(null); if (!currentPassword || !newPassword || !confirmPassword) { setError(t("errors.requiredField")); return; } if (newPassword !== confirmPassword) { setError(t("common.passwordsDoNotMatch")); return; } if (newPassword.length < 6) { setError(t("common.passwordMinLength")); return; } setLoading(true); try { await changePassword(currentPassword, newPassword); toast.success(t("profile.passwordChangedSuccess")); window.location.reload(); } catch (err: unknown) { const error = err as { response?: { data?: { error?: string } } }; setError( error?.response?.data?.error || t("profile.failedToChangePassword"), ); } finally { setLoading(false); } } const Spinner = ( ); return ( {t("common.password")} {t("common.changeAccountPassword")}
setCurrentPassword(e.target.value)} disabled={loading} autoComplete="current-password" />
setNewPassword(e.target.value)} disabled={loading} autoComplete="new-password" />
setConfirmPassword(e.target.value)} disabled={loading} autoComplete="new-password" />
{error && ( Error {error} )}
); }