import * as React from "react"; import { useEffect, useRef, useState } from "react"; import { Typography, TextField, Button, Box } from "@mui/material"; import WarningAmberIcon from "@mui/icons-material/WarningAmber"; import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutlineOutlined"; import { useParams, useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import accountApi from "../app/AccountApi"; import AvatarBox from "./AvatarBox"; import routes from "./routes"; // PasswordReset is the magic-link landing page for setting a new password. There is no // pre-validation: the form renders directly and an invalid/expired token surfaces as an error on // submit. The raw token is stripped from the URL on load (kept out of history / Referer). const PasswordReset = () => { const { t } = useTranslation(); const { token: tokenParam } = useParams(); const navigate = useNavigate(); const token = useRef(tokenParam); const [password, setPassword] = useState(""); const [confirm, setConfirm] = useState(""); const [error, setError] = useState(""); const [sending, setSending] = useState(false); const [done, setDone] = useState(false); useEffect(() => { // Strip the token from the URL bar immediately (keep it out of history / Referer) window.history.replaceState(null, "", routes.login); }, []); const handleSubmit = async (event) => { event.preventDefault(); try { setSending(true); setError(""); await accountApi.resetPassword(token.current, password); setDone(true); } catch (e) { console.log(`[PasswordReset] Reset failed`, e); setError(t("reset_password_form_error_invalid")); } finally { setSending(false); } }; if (done) { return ( {t("reset_password_success_title")} {t("reset_password_success_description")} ); } return ( {t("reset_password_title")} setPassword(ev.target.value)} autoComplete="new-password" autoFocus /> setConfirm(ev.target.value)} autoComplete="new-password" /> {error && ( {error} )} ); }; export default PasswordReset;