|
| 1 | +import { useNavigate, useParams } from "react-router-dom"; |
| 2 | +import AppMargin from "../../components/AppMargin"; |
| 3 | +import { Box, Button, Stack, TextField, Typography } from "@mui/material"; |
| 4 | +import { useEffect, useState } from "react"; |
| 5 | +import { userClient } from "../../utils/api"; |
| 6 | +import classes from "./index.module.css"; |
| 7 | +import { toast } from "react-toastify"; |
| 8 | + |
| 9 | +const EmailVerification: React.FC = () => { |
| 10 | + const { userId } = useParams<{ userId: string }>(); |
| 11 | + const [token, setToken] = useState(""); |
| 12 | + const [email, setEmail] = useState(""); |
| 13 | + const navigate = useNavigate(); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + userClient |
| 17 | + .get(`/users/${userId}/`) |
| 18 | + .then((res) => { |
| 19 | + setEmail(res.data.data.email); |
| 20 | + }) |
| 21 | + .catch((err) => console.error(err)); |
| 22 | + }, []); |
| 23 | + |
| 24 | + const handleResend = () => { |
| 25 | + userClient |
| 26 | + .post(`/users/send-verification-email`, { email }) |
| 27 | + .catch((err) => console.error(err)); |
| 28 | + }; |
| 29 | + |
| 30 | + const handleVerify = (e: React.FormEvent<HTMLFormElement>) => { |
| 31 | + e.preventDefault(); |
| 32 | + userClient |
| 33 | + .get(`/users/verify-email/${email}/${token}`) |
| 34 | + .then((res) => { |
| 35 | + navigate("/auth/login"); |
| 36 | + toast.success(res.data.message); |
| 37 | + }) |
| 38 | + .catch((err) => console.error(err)); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Box |
| 43 | + sx={{ |
| 44 | + display: "flex", |
| 45 | + flexDirection: "column", |
| 46 | + minHeight: "100vh", |
| 47 | + justifyContent: "center", |
| 48 | + alignItems: "center", |
| 49 | + }} |
| 50 | + > |
| 51 | + <AppMargin classname={`${classes.fullheight}`}> |
| 52 | + <Box |
| 53 | + sx={(theme) => ({ |
| 54 | + textAlign: "center", |
| 55 | + border: `1px solid ${theme.palette.divider}`, |
| 56 | + borderRadius: theme.spacing(1), |
| 57 | + width: "40vw", |
| 58 | + padding: theme.spacing(4), |
| 59 | + })} |
| 60 | + > |
| 61 | + <Typography variant="h5">Verify your email address</Typography> |
| 62 | + <Typography sx={(theme) => ({ margin: theme.spacing(2, 0) })}> |
| 63 | + An account verification token has been sent to your email. |
| 64 | + </Typography> |
| 65 | + <form onSubmit={handleVerify}> |
| 66 | + <TextField |
| 67 | + fullWidth |
| 68 | + margin="normal" |
| 69 | + label="Token" |
| 70 | + value={token} |
| 71 | + onChange={(e) => setToken(e.target.value)} |
| 72 | + /> |
| 73 | + <Stack |
| 74 | + direction="row" |
| 75 | + spacing={2} |
| 76 | + sx={(theme) => ({ marginTop: theme.spacing(4) })} |
| 77 | + > |
| 78 | + <Button |
| 79 | + fullWidth |
| 80 | + variant="contained" |
| 81 | + color="secondary" |
| 82 | + onClick={handleResend} |
| 83 | + > |
| 84 | + Resend |
| 85 | + </Button> |
| 86 | + <Button fullWidth variant="contained" type="submit"> |
| 87 | + Verify |
| 88 | + </Button> |
| 89 | + </Stack> |
| 90 | + </form> |
| 91 | + </Box> |
| 92 | + </AppMargin> |
| 93 | + </Box> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default EmailVerification; |
0 commit comments