Debian Codegraph @

web/src/components/PasswordReset.jsx

raw
1 import * as React from "react";
2 import { useEffect, useRef, useState } from "react";
3 import { Typography, TextField, Button, Box } from "@mui/material";
4 import WarningAmberIcon from "@mui/icons-material/WarningAmber";
5 import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutlineOutlined";
6 import { useParams, useNavigate } from "react-router-dom";
7 import { useTranslation } from "react-i18next";
8 import accountApi from "../app/AccountApi";
9 import AvatarBox from "./AvatarBox";
10 import routes from "./routes";
11
12 // PasswordReset is the magic-link landing page for setting a new password. There is no
13 // pre-validation: the form renders directly and an invalid/expired token surfaces as an error on
14 // submit. The raw token is stripped from the URL on load (kept out of history / Referer).
15 const PasswordReset = () => {
16 const { t } = useTranslation();
17 const { token: tokenParam } = useParams();
18 const navigate = useNavigate();
19 const token = useRef(tokenParam);
20 const [password, setPassword] = useState("");
21 const [confirm, setConfirm] = useState("");
22 const [error, setError] = useState("");
23 const [sending, setSending] = useState(false);
24 const [done, setDone] = useState(false);
25
26 useEffect(() => {
27 // Strip the token from the URL bar immediately (keep it out of history / Referer)
28 window.history.replaceState(null, "", routes.login);
29 }, []);
30
31 const handleSubmit = async (event) => {
32 event.preventDefault();
33 try {
34 setSending(true);
35 setError("");
36 await accountApi.resetPassword(token.current, password);
37 setDone(true);
38 } catch (e) {
39 console.log(`[PasswordReset] Reset failed`, e);
40 setError(t("reset_password_form_error_invalid"));
41 } finally {
42 setSending(false);
43 }
44 };
45
46 if (done) {
47 return (
48 <AvatarBox>
49 <Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
50 <CheckCircleOutlineIcon color="success" sx={{ fontSize: 28 }} />
51 <Typography sx={{ typography: "h6" }}>{t("reset_password_success_title")}</Typography>
52 </Box>
53 <Typography sx={{ mt: 1, textAlign: "center" }}>{t("reset_password_success_description")}</Typography>
54 <Button onClick={() => navigate(routes.login)} variant="contained" sx={{ mt: 2 }}>
55 {t("login_form_button_submit")}
56 </Button>
57 </AvatarBox>
58 );
59 }
60
61 return (
62 <AvatarBox>
63 <Typography sx={{ typography: "h6" }}>{t("reset_password_title")}</Typography>
64 <Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}>
65 <TextField
66 margin="dense"
67 required
68 fullWidth
69 name="password"
70 label={t("reset_password_form_password")}
71 type="password"
72 id="password"
73 value={password}
74 onChange={(ev) => setPassword(ev.target.value)}
75 autoComplete="new-password"
76 autoFocus
77 />
78 <TextField
79 margin="dense"
80 required
81 fullWidth
82 name="confirm"
83 label={t("reset_password_form_confirm")}
84 type="password"
85 id="confirm"
86 value={confirm}
87 onChange={(ev) => setConfirm(ev.target.value)}
88 autoComplete="new-password"
89 />
90 <Button
91 type="submit"
92 fullWidth
93 variant="contained"
94 disabled={sending || password === "" || confirm === "" || password !== confirm}
95 sx={{ mt: 2, mb: 2 }}
96 >
97 {t("reset_password_form_button_submit")}
98 </Button>
99 {error && (
100 <Box sx={{ mb: 1, display: "flex", flexGrow: 1, justifyContent: "center" }}>
101 <WarningAmberIcon color="error" sx={{ mr: 1 }} />
102 <Typography sx={{ color: "error.main" }}>{error}</Typography>
103 </Box>
104 )}
105 </Box>
106 </AvatarBox>
107 );
108 };
109
110 export default PasswordReset;