| 1 |
import * as React from "react"; |
| 2 |
import { useEffect, useRef, useState } from "react"; |
| 3 |
import { Typography, Button, Box, CircularProgress } from "@mui/material"; |
| 4 |
import CheckCircleOutlineIcon from "@mui/icons-material/CheckCircleOutlineOutlined"; |
| 5 |
import ErrorOutlineIcon from "@mui/icons-material/ErrorOutlineOutlined"; |
| 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 |
// Verification states for the email-verify landing page |
| 13 |
const STATUS_VERIFYING = "verifying"; |
| 14 |
const STATUS_SUCCESS = "success"; |
| 15 |
const STATUS_ERROR = "error"; |
| 16 |
|
| 17 |
// EmailVerify is the magic-link landing page for email verification. It performs the verification |
| 18 |
// via a POST (the GET that loads this page has no side effects, so link prefetchers / scanners |
| 19 |
// cannot consume the single-use token). The raw token is stripped from the URL on load to keep |
| 20 |
// it out of browser history and Referer headers. |
| 21 |
const EmailVerify = () => { |
| 22 |
const { t } = useTranslation(); |
| 23 |
const { token } = useParams(); |
| 24 |
const navigate = useNavigate(); |
| 25 |
const [status, setStatus] = useState(STATUS_VERIFYING); |
| 26 |
const ran = useRef(false); |
| 27 |
|
| 28 |
useEffect(() => { |
| 29 |
if (ran.current) { |
| 30 |
return; // Guard against double-invoke (e.g. React StrictMode) consuming the token twice |
| 31 |
} |
| 32 |
ran.current = true; |
| 33 |
// Strip the token from the URL immediately (keep it out of history / Referer) |
| 34 |
window.history.replaceState(null, "", routes.account); |
| 35 |
(async () => { |
| 36 |
try { |
| 37 |
await accountApi.verifyEmailToken(token); |
| 38 |
setStatus(STATUS_SUCCESS); |
| 39 |
} catch (e) { |
| 40 |
console.log(`[EmailVerify] Verification failed`, e); |
| 41 |
setStatus(STATUS_ERROR); |
| 42 |
} |
| 43 |
})(); |
| 44 |
}, [token]); |
| 45 |
|
| 46 |
return ( |
| 47 |
<AvatarBox> |
| 48 |
{status === STATUS_VERIFYING && ( |
| 49 |
<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}> |
| 50 |
<CircularProgress size={24} /> |
| 51 |
<Typography sx={{ typography: "h6" }}>{t("email_verify_progress_title")}</Typography> |
| 52 |
</Box> |
| 53 |
)} |
| 54 |
{status === STATUS_SUCCESS && ( |
| 55 |
<> |
| 56 |
<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}> |
| 57 |
<CheckCircleOutlineIcon color="success" sx={{ fontSize: 28 }} /> |
| 58 |
<Typography sx={{ typography: "h6" }}>{t("email_verify_success_title")}</Typography> |
| 59 |
</Box> |
| 60 |
<Typography sx={{ mt: 1, textAlign: "center" }}>{t("email_verify_success_description")}</Typography> |
| 61 |
<Button onClick={() => navigate(routes.account)} variant="contained" sx={{ mt: 2 }}> |
| 62 |
{t("email_verify_button_account")} |
| 63 |
</Button> |
| 64 |
</> |
| 65 |
)} |
| 66 |
{status === STATUS_ERROR && ( |
| 67 |
<> |
| 68 |
<Box sx={{ display: "flex", alignItems: "center", gap: 1 }}> |
| 69 |
<ErrorOutlineIcon color="error" sx={{ fontSize: 28 }} /> |
| 70 |
<Typography sx={{ typography: "h6" }}>{t("email_verify_error_title")}</Typography> |
| 71 |
</Box> |
| 72 |
<Typography sx={{ mt: 1, textAlign: "center" }}>{t("email_verify_error_description")}</Typography> |
| 73 |
<Button onClick={() => navigate(routes.account)} variant="contained" sx={{ mt: 2 }}> |
| 74 |
{t("email_verify_button_account")} |
| 75 |
</Button> |
| 76 |
</> |
| 77 |
)} |
| 78 |
</AvatarBox> |
| 79 |
); |
| 80 |
}; |
| 81 |
|
| 82 |
export default EmailVerify; |