Skip to content

Commit

Permalink
Fix email verify issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Somnath-Chattaraj committed Oct 20, 2024
1 parent a4fde59 commit c5d0302
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
33 changes: 30 additions & 3 deletions backend/src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,27 @@ const registerUser = asyncHandler(async (req: Request, res: Response) => {
res.status(201).json({ message: "User created" });
}
});

const resendURL = asyncHandler(async (req: Request, res: Response) => {
const {email, password} = req.body;
if (!email || !password) {
res.status(400).json({ message: "Please provide all fields" });
return;
}
const user = await prisma.user.findUnique({
where: {
email,
},
});
if (!user) {
res.status(404).json({ message: "User not found" });
return;
}
if (!user.password) {
res.status(401).json({ message: "Logged in with Google Or Github" });
return;
}

})
const verifyUser = asyncHandler(async (req: Request, res: Response) => {
const token = req.params.token;
if (!token) {
Expand All @@ -240,7 +260,7 @@ const verifyUser = asyncHandler(async (req: Request, res: Response) => {
const { sub, exp } = jwt.verify(token, process.env.SECRET);
// @ts-ignore
if (exp < Date.now()) {
res.status(400).json({ message: "Token expired" });
res.status(400).json({ message: "Token expired. Login to verify your email" });
return;
}
const user = await prisma.user.findUnique({
Expand All @@ -252,6 +272,7 @@ const verifyUser = asyncHandler(async (req: Request, res: Response) => {
res.status(404).json({ message: "User not found" });
return;
}

if (user.emailVerified) {
res.status(400).json({ message: "User already verified" });
return;
Expand Down Expand Up @@ -290,7 +311,13 @@ const loginUser = asyncHandler(async (req: Request, res: Response) => {
return;
}
if (!user.emailVerified) {
res.status(401).json({ message: "Email not verified" });
const exp = Date.now() + 1000 * 60 * 5;
// @ts-ignore
const token = jwt.sign({ sub: user.user_id, exp }, process.env.SECRET);
const url = `${process.env.BACKEND_URL}/api/user/verify/${token}`;
const htmlContent = `<a href="${url}">Verify using this link</a>`;
sendMail(htmlContent, email);
res.status(201).json({ message: "Email Sent" })
return;
}
const match = await bcrypt.compare(password, user.password);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ const LoginPage = () => {
{ withCredentials: true }
);
setLoading(false);
if (response.status == 201) {
toast({
title: "Email not verified.",
description: "Please check your email for verification.",
status: "error",
duration: 3000,
isClosable: true,
});
} else {
toast({
title: "Login successful.",
description: "You are being redirected to the posts page.",
Expand All @@ -165,6 +174,7 @@ const LoginPage = () => {
isClosable: true,
});
navigate("/posts");
}
} catch (error) {
setLoading(false);
toast({
Expand Down

0 comments on commit c5d0302

Please sign in to comment.