Debian Codegraph @

web/src/components/PasswordResetRequest.jsx

raw
1 import * as React from "react";
2 import { useState } from "react";
3 import { TextField, Button, Box, Typography } from "@mui/material";
4 import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutlineOutlined";
5 import { NavLink } from "react-router-dom";
6 import { useTranslation } from "react-i18next";
7 import accountApi from "../app/AccountApi";
8 import AvatarBox from "./AvatarBox";
9 import routes from "./routes";
10
11 // PasswordResetRequest is the standalone "request a password reset" page, reached from the login page.
12 // It collects a username/email and asks the server to email a reset link. The response is uniform,
13 // so the page always shows the same confirmation. Completing the reset happens on the separate
14 // PasswordReset landing page that the emailed link points to.
15 const PasswordResetRequest = () => {
16 const { t } = useTranslation();
17 const [identifier, setIdentifier] = useState("");
18 const [sending, setSending] = useState(false);
19 const [sent, setSent] = useState(false);
20
21 const handleSubmit = async (event) => {
22 event.preventDefault();
23 try {
24 setSending(true);
25 await accountApi.requestPasswordReset(identifier);
26 } catch (e) {
27 console.log(`[PasswordResetRequest] Request failed`, e);
28 } finally {
29 setSending(false);
30 setSent(true); // Uniform outcome regardless of success/failure (enumeration-safe)
31 }
32 };
33
34 if (!config.enable_reset_password) {
35 return (
36 <AvatarBox>
37 <Typography sx={{ typography: "h6" }}>{t("reset_password_disabled")}</Typography>
38 <Typography sx={{ mt: 2 }}>
39 <NavLink to={routes.login} variant="body1">
40 {t("reset_password_back_to_login")}
41 </NavLink>
42 </Typography>
43 </AvatarBox>
44 );
45 }
46
47 if (sent) {
48 return (
49 <AvatarBox>
50 <Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
51 <CheckCircleOutlineIcon color="success" sx={{ fontSize: 28 }} />
52 <Typography sx={{ typography: "h6" }}>{t("reset_password_sent_title")}</Typography>
53 </Box>
54 <Typography sx={{ mt: 1, textAlign: "center" }}>{t("reset_password_sent_description")}</Typography>
55 <Typography sx={{ mt: 2, mb: 4 }}>
56 <NavLink to={routes.login} variant="body1">
57 {t("reset_password_back_to_login")}
58 </NavLink>
59 </Typography>
60 </AvatarBox>
61 );
62 }
63
64 return (
65 <AvatarBox>
66 <Typography sx={{ typography: "h6" }}>{t("reset_password_request_title")}</Typography>
67 <Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}>
68 <Typography sx={{ mt: 1 }}>{t("reset_password_request_description")}</Typography>
69 <Typography sx={{ mt: 1, mb: 1.5, fontWeight: "bold" }}>{t("reset_password_request_primary_required")}</Typography>
70 <TextField
71 margin="dense"
72 required
73 fullWidth
74 id="identifier"
75 label={t("reset_password_request_identifier_label")}
76 name="identifier"
77 value={identifier}
78 onChange={(ev) => setIdentifier(ev.target.value.trim())}
79 autoFocus
80 />
81 <Button type="submit" fullWidth variant="contained" disabled={sending || identifier === ""} sx={{ mt: 2, mb: 2 }}>
82 {t("reset_password_request_button_submit")}
83 </Button>
84 </Box>
85 {config.enable_login && (
86 <Typography sx={{ mb: 4 }}>
87 <NavLink to={routes.login} variant="body1">
88 {t("reset_password_back_to_login")}
89 </NavLink>
90 </Typography>
91 )}
92 </AvatarBox>
93 );
94 };
95
96 export default PasswordResetRequest;