Skip to content

Commit fce340e

Browse files
committed
Redirect users to email verification page
1 parent 7e9b2bd commit fce340e

File tree

4 files changed

+60
-40
lines changed

4 files changed

+60
-40
lines changed

backend/user-service/controller/user-controller.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,10 @@ export const verifyUser = async (
155155

156156
const updatedUser = await _updateUserVerification(email);
157157
if (!updatedUser) {
158-
return res.status(404).json({ message: `User ${email} not verified.` });
158+
return res.status(404).json({ message: `User not verified.` });
159159
}
160160

161-
return res
162-
.status(200)
163-
.json({ message: `User ${email} verified successfully.` });
161+
return res.status(200).json({ message: `User verified successfully.` });
164162
} catch (error) {
165163
return res
166164
.status(500)

frontend/src/App.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Routes, Route } from "react-router-dom";
1+
import { Routes, Route, Navigate } from "react-router-dom";
22

33
import NewQuestion from "./pages/NewQuestion";
44
import QuestionDetail from "./pages/QuestionDetail";
@@ -64,10 +64,14 @@ function App() {
6464
</Route>
6565
<Route path="*" element={<PageNotFound />} />
6666
</Route>
67-
<Route path="/auth">
67+
<Route path="auth">
68+
<Route index element={<Navigate to="/auth/login" />} />
6869
<Route path="signup" element={<SignUp />} />
6970
<Route path="login" element={<LogIn />} />
70-
<Route path="verifyEmail/:userId" element={<EmailVerification />} />
71+
<Route
72+
path="verifyEmail/:userId?"
73+
element={<EmailVerification />}
74+
/>
7175
</Route>
7276
</Routes>
7377
</MatchProvider>

frontend/src/contexts/AuthContext.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6767
.then(() => userClient.post("users/send-verification-email", { email }))
6868
.then((res) => {
6969
navigate(`/auth/verifyEmail/${res.data.data.id}`);
70-
// navigate("/login");
71-
// toast.success(res.data.message);
7270
})
7371
.catch((err) => {
7472
setUser(null);
@@ -89,6 +87,9 @@ const AuthProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
8987
navigate("/home");
9088
})
9189
.catch((err) => {
90+
if (err.response?.data.message === "User not verified.") {
91+
navigate(`/auth/verifyEmail`);
92+
}
9293
setUser(null);
9394
toast.error(err.response?.data.message || err.message);
9495
});

frontend/src/pages/EmailVerification/index.tsx

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import classes from "./index.module.css";
77
import { toast } from "react-toastify";
88

99
const EmailVerification: React.FC = () => {
10-
const { userId } = useParams<{ userId: string }>();
10+
const { userId } = useParams<{ userId?: string }>();
1111
const [token, setToken] = useState("");
1212
const [email, setEmail] = useState("");
1313
const navigate = useNavigate();
@@ -19,16 +19,17 @@ const EmailVerification: React.FC = () => {
1919
setEmail(res.data.data.email);
2020
})
2121
.catch((err) => console.error(err));
22+
// eslint-disable-next-line react-hooks/exhaustive-deps
2223
}, []);
2324

24-
const handleResend = () => {
25+
const handleSendEmail = () => {
2526
userClient
2627
.post(`/users/send-verification-email`, { email })
28+
.then((res) => toast.success(res.data.message))
2729
.catch((err) => console.error(err));
2830
};
2931

30-
const handleVerify = (e: React.FormEvent<HTMLFormElement>) => {
31-
e.preventDefault();
32+
const handleVerifyAcc = () => {
3233
userClient
3334
.get(`/users/verify-email/${email}/${token}`)
3435
.then((res) => {
@@ -59,35 +60,51 @@ const EmailVerification: React.FC = () => {
5960
})}
6061
>
6162
<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
63+
{userId ? (
64+
<Typography sx={(theme) => ({ margin: theme.spacing(2, 0) })}>
65+
An account verification token has been sent to your email.
66+
</Typography>
67+
) : (
68+
<Typography sx={(theme) => ({ margin: theme.spacing(2, 0) })}>
69+
An account verification token will be sent to your email.
70+
</Typography>
71+
)}
72+
<TextField
73+
fullWidth
74+
margin="normal"
75+
label="Email"
76+
value={email}
77+
onChange={(e) => setEmail(e.target.value)}
78+
slotProps={{
79+
input: {
80+
endAdornment: <Button onClick={handleSendEmail}>Send</Button>,
81+
},
82+
}}
83+
/>
84+
<TextField
85+
fullWidth
86+
margin="normal"
87+
label="Token"
88+
value={token}
89+
onChange={(e) => setToken(e.target.value)}
90+
/>
91+
<Stack
92+
direction="row"
93+
spacing={2}
94+
sx={(theme) => ({ marginTop: theme.spacing(4) })}
95+
>
96+
<Button
6797
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) })}
98+
variant="contained"
99+
color="secondary"
100+
onClick={handleSendEmail}
77101
>
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>
102+
Resend
103+
</Button>
104+
<Button fullWidth variant="contained" onClick={handleVerifyAcc}>
105+
Verify
106+
</Button>
107+
</Stack>
91108
</Box>
92109
</AppMargin>
93110
</Box>

0 commit comments

Comments
 (0)