diff --git a/back-end/routes/forgotPasswordRoute.js b/back-end/routes/forgotPasswordRoute.js index 6ffcb79..6e01e5e 100644 --- a/back-end/routes/forgotPasswordRoute.js +++ b/back-end/routes/forgotPasswordRoute.js @@ -2,36 +2,52 @@ const express = require("express"); const router = express.Router(); const { User } = require("../models/User.js"); const bcrypt = require("bcrypt"); +const { body, validationResult } = require("express-validator"); + +router.post( + "/", + [ + body("email").not().isEmpty().withMessage("Email is required"), + body("username").not().isEmpty().withMessage("Username is required"), + body("newPassword").not().isEmpty().withMessage("New Password is required"), + ], + async (req, res) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ success: false, errors: errors.array() }); + } -router.post("/", async (req, res) => { - const username = req.body.username; - const newPassword = req.body.newPassword; - const email = req.body.email; + const username = req.body.username; + const newPassword = req.body.newPassword; + const email = req.body.email; - try { - const user = await User.findOne({ - $and: [{ $or: [{ username: username }, { email: email }] }], - }); + try { + const user = await User.findOne({ + $and: [{ $or: [{ username: username }, { email: email }] }], + }); - console.log(user); + console.log(user); - if (!user) { - console.error(`User not found.`); - return res - .status(404) - .json({ success: false, message: "User not found." }); - } + if (!user) { + console.error(`User not found.`); + return res + .status(404) + .json({ success: false, message: "User not found." }); + } - const hashedPassword = await bcrypt.hash(newPassword, 10); - user.password = hashedPassword; + const hashedPassword = await bcrypt.hash(newPassword, 10); + user.password = hashedPassword; - await user.save(); + await user.save(); - res.json({ success: true, message: "Password updated successfully." }); - } catch (error) { - console.error(`Error updating password: ${error}`); - res.status(500).json({ success: false, message: "Internal server error." }); + res.json({ success: true, message: "Password updated successfully." }); + } catch (error) { + console.error(`Error updating password: ${error}`); + res + .status(500) + .json({ success: false, message: "Internal server error." }); + } } -}); +); module.exports = router; diff --git a/back-end/routes/signupRoute.js b/back-end/routes/signupRoute.js index 102429e..b51cf49 100644 --- a/back-end/routes/signupRoute.js +++ b/back-end/routes/signupRoute.js @@ -12,6 +12,11 @@ router.post( ], async (req, res, next) => { + const errors = validationResult(req); + if (!errors.isEmpty()) { + return res.status(400).json({ success: false, errors: errors.array() }); + } + const username = req.body.username; const password = req.body.password; const email = req.body.email; diff --git a/front-end/src/components/ForgotPassword.jsx b/front-end/src/components/ForgotPassword.jsx index 8ef99cc..7cba671 100644 --- a/front-end/src/components/ForgotPassword.jsx +++ b/front-end/src/components/ForgotPassword.jsx @@ -58,7 +58,7 @@ const ForgotPassword = () => { Email {

Login

{showNotification && (
-

You have been successfully registered. Please log in.

+ {/*

You have been successfully registered. Please log in.

*/} +

Please log in again!

)} {errorMessage ?

{errorMessage}

: ""}