2025-08-31 00:42:50 -05:00
|
|
|
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card.tsx";
|
|
|
|
|
import {Key} from "lucide-react";
|
2025-08-31 20:18:08 -05:00
|
|
|
import React, {useState} from "react";
|
|
|
|
|
import {completePasswordReset, initiatePasswordReset, verifyPasswordResetCode} from "@/ui/main-axios.ts";
|
|
|
|
|
import {Label} from "@/components/ui/label.tsx";
|
|
|
|
|
import {Input} from "@/components/ui/input.tsx";
|
|
|
|
|
import {Button} from "@/components/ui/button.tsx";
|
|
|
|
|
import {Alert, AlertDescription, AlertTitle} from "@/components/ui/alert.tsx";
|
2025-09-03 00:14:49 -05:00
|
|
|
import {toast} from "sonner";
|
|
|
|
|
import {useTranslation} from "react-i18next";
|
2025-08-31 20:18:08 -05:00
|
|
|
|
|
|
|
|
interface PasswordResetProps {
|
|
|
|
|
userInfo: {
|
|
|
|
|
username: string;
|
|
|
|
|
is_admin: boolean;
|
|
|
|
|
is_oidc: boolean;
|
|
|
|
|
totp_enabled: boolean;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function PasswordReset({userInfo}: PasswordResetProps) {
|
2025-09-03 00:14:49 -05:00
|
|
|
const {t} = useTranslation();
|
2025-08-31 20:18:08 -05:00
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const [resetStep, setResetStep] = useState<"initiate" | "verify" | "newPassword">("initiate");
|
|
|
|
|
const [resetCode, setResetCode] = useState("");
|
|
|
|
|
const [newPassword, setNewPassword] = useState("");
|
|
|
|
|
const [confirmPassword, setConfirmPassword] = useState("");
|
|
|
|
|
const [tempToken, setTempToken] = useState("");
|
|
|
|
|
const [resetLoading, setResetLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
async function handleInitiatePasswordReset() {
|
|
|
|
|
setError(null);
|
|
|
|
|
setResetLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const result = await initiatePasswordReset(userInfo.username);
|
|
|
|
|
setResetStep("verify");
|
|
|
|
|
setError(null);
|
|
|
|
|
} catch (err: any) {
|
2025-09-03 00:14:49 -05:00
|
|
|
setError(err?.response?.data?.error || err?.message || t('common.failedToInitiatePasswordReset'));
|
2025-08-31 20:18:08 -05:00
|
|
|
} finally {
|
|
|
|
|
setResetLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resetPasswordState() {
|
|
|
|
|
setResetStep("initiate");
|
|
|
|
|
setResetCode("");
|
|
|
|
|
setNewPassword("");
|
|
|
|
|
setConfirmPassword("");
|
|
|
|
|
setTempToken("");
|
|
|
|
|
setError(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleVerifyResetCode() {
|
|
|
|
|
setError(null);
|
|
|
|
|
setResetLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const response = await verifyPasswordResetCode(userInfo.username, resetCode);
|
|
|
|
|
setTempToken(response.tempToken);
|
|
|
|
|
setResetStep("newPassword");
|
|
|
|
|
setError(null);
|
|
|
|
|
} catch (err: any) {
|
2025-09-03 00:14:49 -05:00
|
|
|
setError(err?.response?.data?.error || t('common.failedToVerifyResetCode'));
|
2025-08-31 20:18:08 -05:00
|
|
|
} finally {
|
|
|
|
|
setResetLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleCompletePasswordReset() {
|
|
|
|
|
setError(null);
|
|
|
|
|
setResetLoading(true);
|
|
|
|
|
|
|
|
|
|
if (newPassword !== confirmPassword) {
|
2025-09-03 00:14:49 -05:00
|
|
|
setError(t('common.passwordsDoNotMatch'));
|
2025-08-31 20:18:08 -05:00
|
|
|
setResetLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newPassword.length < 6) {
|
2025-09-03 00:14:49 -05:00
|
|
|
setError(t('common.passwordMinLength'));
|
2025-08-31 20:18:08 -05:00
|
|
|
setResetLoading(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await completePasswordReset(userInfo.username, tempToken, newPassword);
|
|
|
|
|
|
2025-09-03 00:14:49 -05:00
|
|
|
toast.success(t('common.passwordResetSuccess'));
|
|
|
|
|
resetPasswordState();
|
2025-08-31 20:18:08 -05:00
|
|
|
} catch (err: any) {
|
2025-09-03 00:14:49 -05:00
|
|
|
setError(err?.response?.data?.error || t('common.failedToCompletePasswordReset'));
|
2025-08-31 20:18:08 -05:00
|
|
|
} finally {
|
|
|
|
|
setResetLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Spinner = (
|
|
|
|
|
<svg className="animate-spin mr-2 h-4 w-4 text-white inline-block" viewBox="0 0 24 24">
|
|
|
|
|
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" fill="none"/>
|
|
|
|
|
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8v4a4 4 0 00-4 4H4z"/>
|
|
|
|
|
</svg>
|
|
|
|
|
);
|
2025-08-31 00:42:50 -05:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-2">
|
|
|
|
|
<Key className="w-5 h-5"/>
|
2025-09-03 00:14:49 -05:00
|
|
|
{t('common.password')}
|
2025-08-31 00:42:50 -05:00
|
|
|
</CardTitle>
|
|
|
|
|
<CardDescription>
|
2025-09-03 00:14:49 -05:00
|
|
|
{t('common.changeAccountPassword')}
|
2025-08-31 00:42:50 -05:00
|
|
|
</CardDescription>
|
|
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2025-08-31 20:18:08 -05:00
|
|
|
<>
|
2025-09-03 00:14:49 -05:00
|
|
|
{resetStep === "initiate" && (
|
2025-08-31 20:18:08 -05:00
|
|
|
<>
|
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
className="w-full h-11 text-base font-semibold"
|
|
|
|
|
disabled={resetLoading || !userInfo.username.trim()}
|
|
|
|
|
onClick={handleInitiatePasswordReset}
|
|
|
|
|
>
|
2025-09-03 00:14:49 -05:00
|
|
|
{resetLoading ? Spinner : t('common.sendResetCode')}
|
2025-08-31 20:18:08 -05:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{resetStep === "verify" && (
|
|
|
|
|
<>
|
|
|
|
|
<div className="text-center text-muted-foreground mb-4">
|
2025-09-03 00:14:49 -05:00
|
|
|
<p>{t('common.enterSixDigitCode')} <strong>{userInfo.username}</strong></p>
|
2025-08-31 20:18:08 -05:00
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
<div className="flex flex-col gap-2">
|
2025-09-03 00:14:49 -05:00
|
|
|
<Label htmlFor="reset-code">{t('common.resetCode')}</Label>
|
2025-08-31 20:18:08 -05:00
|
|
|
<Input
|
|
|
|
|
id="reset-code"
|
|
|
|
|
type="text"
|
|
|
|
|
required
|
|
|
|
|
maxLength={6}
|
|
|
|
|
className="h-11 text-base text-center text-lg tracking-widest"
|
|
|
|
|
value={resetCode}
|
|
|
|
|
onChange={e => setResetCode(e.target.value.replace(/\D/g, ''))}
|
|
|
|
|
disabled={resetLoading}
|
2025-09-03 00:14:49 -05:00
|
|
|
placeholder={t('placeholders.enterCode')}
|
2025-08-31 20:18:08 -05:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
className="w-full h-11 text-base font-semibold"
|
|
|
|
|
disabled={resetLoading || resetCode.length !== 6}
|
|
|
|
|
onClick={handleVerifyResetCode}
|
|
|
|
|
>
|
2025-09-03 00:14:49 -05:00
|
|
|
{resetLoading ? Spinner : t('common.verifyCode')}
|
2025-08-31 20:18:08 -05:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
className="w-full h-11 text-base font-semibold"
|
|
|
|
|
disabled={resetLoading}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setResetStep("initiate");
|
|
|
|
|
setResetCode("");
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-09-03 00:14:49 -05:00
|
|
|
{t('common.back')}
|
2025-08-31 20:18:08 -05:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-09-03 00:14:49 -05:00
|
|
|
{resetStep === "newPassword" && (
|
2025-08-31 20:18:08 -05:00
|
|
|
<>
|
|
|
|
|
<div className="text-center text-muted-foreground mb-4">
|
2025-09-03 00:14:49 -05:00
|
|
|
<p>{t('common.enterNewPassword')} <strong>{userInfo.username}</strong></p>
|
2025-08-31 20:18:08 -05:00
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col gap-5">
|
|
|
|
|
<div className="flex flex-col gap-2">
|
2025-09-03 00:14:49 -05:00
|
|
|
<Label htmlFor="new-password">{t('common.newPassword')}</Label>
|
2025-08-31 20:18:08 -05:00
|
|
|
<Input
|
|
|
|
|
id="new-password"
|
|
|
|
|
type="password"
|
|
|
|
|
required
|
|
|
|
|
className="h-11 text-base focus:ring-2 focus:ring-primary/50 transition-all duration-200"
|
|
|
|
|
value={newPassword}
|
|
|
|
|
onChange={e => setNewPassword(e.target.value)}
|
|
|
|
|
disabled={resetLoading}
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col gap-2">
|
2025-09-03 00:14:49 -05:00
|
|
|
<Label htmlFor="confirm-password">{t('common.confirmPassword')}</Label>
|
2025-08-31 20:18:08 -05:00
|
|
|
<Input
|
|
|
|
|
id="confirm-password"
|
|
|
|
|
type="password"
|
|
|
|
|
required
|
|
|
|
|
className="h-11 text-base focus:ring-2 focus:ring-primary/50 transition-all duration-200"
|
|
|
|
|
value={confirmPassword}
|
|
|
|
|
onChange={e => setConfirmPassword(e.target.value)}
|
|
|
|
|
disabled={resetLoading}
|
|
|
|
|
autoComplete="new-password"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
className="w-full h-11 text-base font-semibold"
|
|
|
|
|
disabled={resetLoading || !newPassword || !confirmPassword}
|
|
|
|
|
onClick={handleCompletePasswordReset}
|
|
|
|
|
>
|
2025-09-03 00:14:49 -05:00
|
|
|
{resetLoading ? Spinner : t('common.resetPassword')}
|
2025-08-31 20:18:08 -05:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
className="w-full h-11 text-base font-semibold"
|
|
|
|
|
disabled={resetLoading}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setResetStep("verify");
|
|
|
|
|
setNewPassword("");
|
|
|
|
|
setConfirmPassword("");
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-09-03 00:14:49 -05:00
|
|
|
{t('common.back')}
|
2025-08-31 20:18:08 -05:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
{error && (
|
|
|
|
|
<Alert variant="destructive" className="mt-4">
|
2025-09-03 00:14:49 -05:00
|
|
|
<AlertTitle>{t('common.error')}</AlertTitle>
|
2025-08-31 20:18:08 -05:00
|
|
|
<AlertDescription>{error}</AlertDescription>
|
|
|
|
|
</Alert>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2025-08-31 00:42:50 -05:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
)
|
|
|
|
|
}
|