diff --git a/server/controllers/Auth.js b/server/controllers/Auth.js index 2fa1b24..a344692 100644 --- a/server/controllers/Auth.js +++ b/server/controllers/Auth.js @@ -17,13 +17,16 @@ exports.studentSignup = async (req, res) => { console.log("This is jwt", process.env.JWT_SECRET); try { console.log(req.body); - const { - name, - email, - collegeName, - accountType, - password, - } = await req.body; + const { name, email, collegeName, accountType, password, confirmPassword } = + await req.body; + + if (password !== confirmPassword) { + return res.status(400).json({ + success: false, + message: "Password and Confirm password didn't match, try again", + }); + } + const existingUser = await User.findOne({ email, }); @@ -38,10 +41,7 @@ exports.studentSignup = async (req, res) => { let hashedPassword; try { - hashedPassword = await bcrypt.hash( - password, - 10 - ); + hashedPassword = await bcrypt.hash(password, 10); } catch (error) { console.log(error); return res.status(500).json({ @@ -99,19 +99,10 @@ exports.studentLogin = async (req, res) => { accountType: user.accountType, }; - if ( - await bcrypt.compare( - password, - user.password - ) - ) { - let token = jwt.sign( - payload, - process.env.JWT_SECRET, - { - expiresIn: "2h", - } - ); + if (await bcrypt.compare(password, user.password)) { + let token = jwt.sign(payload, process.env.JWT_SECRET, { + expiresIn: "2h", + }); // creating a session const session = new Session({ @@ -190,9 +181,7 @@ exports.studentLogout = async (req, res) => { const token = req.cookies?.token || - req - ?.header("Authorization") - ?.replace("Bearer ", ""); + req?.header("Authorization")?.replace("Bearer ", ""); if (token) { await Session.findOneAndDelete({ token }); @@ -212,17 +201,11 @@ exports.studentLogout = async (req, res) => { }; // Controller for changing the student password -exports.changeStudentPassword = async ( - req, - res -) => { +exports.changeStudentPassword = async (req, res) => { const { oldPassword, newPassword } = req.body; const user = await User.findById(req.user._id); - const isPasswordCorrect = await bcrypt.compare( - oldPassword, - user.password - ); + const isPasswordCorrect = await bcrypt.compare(oldPassword, user.password); if (!isPasswordCorrect) { return res.status(400).json({ @@ -231,10 +214,7 @@ exports.changeStudentPassword = async ( }); } - const newHashedPassword = await bcrypt.hash( - newPassword, - 10 - ); + const newHashedPassword = await bcrypt.hash(newPassword, 10); user.password = newHashedPassword; user.save(); @@ -250,16 +230,8 @@ exports.changeStudentPassword = async ( exports.canteenSignup = async (req, res) => { console.log("Received signup request with data:", req.body); try { - const { - name, - email, - collegeName, - accountType, - password, - } = req.body; - const existingCanteen = await Canteen.findOne( - { email } - ); + const { name, email, collegeName, accountType, password } = req.body; + const existingCanteen = await Canteen.findOne({ email }); if (existingCanteen) { console.log("User already exists with email:", email); @@ -272,10 +244,7 @@ exports.canteenSignup = async (req, res) => { let hashedPassword; try { - hashedPassword = await bcrypt.hash( - password, - 10 - ); + hashedPassword = await bcrypt.hash(password, 10); } catch (error) { console.error("Error in hashing password:", error); return res.status(500).json({ @@ -293,9 +262,13 @@ exports.canteenSignup = async (req, res) => { }); // Create a token - const token = jwt.sign({ id: canteen._id, email: canteen.email }, process.env.JWT_SECRET, { - expiresIn: '1h', // Set token expiration time as needed - }); + const token = jwt.sign( + { id: canteen._id, email: canteen.email }, + process.env.JWT_SECRET, + { + expiresIn: "1h", // Set token expiration time as needed + } + ); console.log("User created successfully with ID:", canteen._id); return res.status(200).json({ @@ -339,19 +312,10 @@ exports.canteenLogin = async (req, res) => { accountType: canteen.accountType, }; - if ( - await bcrypt.compare( - password, - canteen.password - ) - ) { - let token = jwt.sign( - payload, - process.env.JWT_SECRET, - { - expiresIn: "2h", - } - ); + if (await bcrypt.compare(password, canteen.password)) { + let token = jwt.sign(payload, process.env.JWT_SECRET, { + expiresIn: "2h", + }); canteen = canteen.toObject(); canteen.token = token; console.log(canteen); @@ -432,9 +396,7 @@ exports.canteenLogout = async (req, res) => { const token = req.cookies?.token || - req - ?.header("Authorization") - ?.replace("Bearer ", ""); + req?.header("Authorization")?.replace("Bearer ", ""); if (token) { await Session.findOneAndDelete({ token }); @@ -454,19 +416,11 @@ exports.canteenLogout = async (req, res) => { }; // Canteen Reset Password -exports.changeCanteenPassword = async ( - req, - res -) => { +exports.changeCanteenPassword = async (req, res) => { const { oldPassword, newPassword } = req.body; - const user = await Canteen.findById( - req.user._id - ); + const user = await Canteen.findById(req.user._id); - const isPasswordCorrect = await bcrypt.compare( - oldPassword, - user.password - ); + const isPasswordCorrect = await bcrypt.compare(oldPassword, user.password); if (!isPasswordCorrect) { return res.status(400).json({ @@ -475,10 +429,7 @@ exports.changeCanteenPassword = async ( }); } - const newHashedPassword = await bcrypt.hash( - newPassword, - 10 - ); + const newHashedPassword = await bcrypt.hash(newPassword, 10); user.password = newHashedPassword; user.save(); @@ -493,9 +444,7 @@ exports.changeCanteenPassword = async ( exports.forgotPassword = async (req, res) => { try { const { email } = req.body; - const existingUser = await findUserByEmail( - email - ); + const existingUser = await findUserByEmail(email); if (!existingUser) { return res.status(400).json({ @@ -503,20 +452,18 @@ exports.forgotPassword = async (req, res) => { message: "User does not exist", }); } else { - const tokenReturn = - forgotPasswordToken(existingUser); + const tokenReturn = forgotPasswordToken(existingUser); // const link = `http://localhost:3000/api/v1/newPassword/${existingUser._id}/${tokenReturn}`; const link = `https://foodies-web-app.vercel.app/api/v1/newPassword/${existingUser._id}/${tokenReturn}`; - const transporter = - nodemailer.createTransport({ - service: "gmail", - auth: { - user: process.env.EMAIL, - pass: process.env.MAILPASS, - }, - }); + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: process.env.EMAIL, + pass: process.env.MAILPASS, + }, + }); const mailOptions = { from: process.env.EMAIL, @@ -539,14 +486,11 @@ exports.forgotPassword = async (req, res) => { `, }; - await transporter.sendMail( - mailOptions, - function (error, info) { - if (error) { - console.log(error); - } + await transporter.sendMail(mailOptions, function (error, info) { + if (error) { + console.log(error); } - ); + }); res.status(201).json({ msg: "You should receive an email", @@ -606,23 +550,16 @@ exports.resetPassword = async (req, res) => { const oldUser = await findUserById(id); if (!oldUser) { - return res - .status(404) - .json("User not found"); + return res.status(404).json("User not found"); } const verify = verifyToken(oldUser, token); if (verify.id !== id) { - return res - .status(201) - .json({ change: false }); + return res.status(201).json({ change: false }); } const salt = await bcrypt.genSalt(10); - const newPassword = await bcrypt.hash( - password, - salt - ); + const newPassword = await bcrypt.hash(password, salt); if (oldUser instanceof User) { await User.findByIdAndUpdate(id, { @@ -636,10 +573,7 @@ exports.resetPassword = async (req, res) => { res.status(201).json({ change: true }); } catch (error) { - console.log( - "Error while changing password: ", - error - ); + console.log("Error while changing password: ", error); res.status(500).json("Some error occurred!"); } }; diff --git a/server/package.json b/server/package.json index c81181f..9875b1c 100644 --- a/server/package.json +++ b/server/package.json @@ -17,14 +17,14 @@ "cors": "^2.8.5", "datauri": "^4.1.0", "dotenv": "^16.4.5", - "express": "^4.18.2", + "express": "^4.19.2", "express-async-handler": "^1.2.0", "faker": "^5.5.3", "jsonwebtoken": "^9.0.2", - "mongoose": "^7.6.2", + "mongoose": "^7.6.13", "multer": "^1.4.5-lts.1", "nodemailer": "^6.9.13", - "nodemon": "^3.0.1", + "nodemon": "^3.1.3", "path": "^0.12.7" } } diff --git a/src/pages/Signup.jsx b/src/pages/Signup.jsx index 15967fc..4d3c2bd 100644 --- a/src/pages/Signup.jsx +++ b/src/pages/Signup.jsx @@ -16,8 +16,9 @@ function Signup() { collegeName: "", accountType: "", password: "", + confirmPassword: "", }); - + const [showconfirmPassword, setshowconfirmPassword] = useState(false); const [showPassword, setShowPassword] = useState(false); const [lowerValidated, setLowerValidated] = useState(false); const [upperValidated, setUpperValidated] = useState(false); @@ -85,12 +86,9 @@ function Signup() { lengthValidated ) { if (formData.accountType === "User") { - - // const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`; - const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`; - // const apiUrl = `http://localhost:8000/api/v1/studentSignUp`; - try { - + const apiUrl = `${process.env.REACT_APP_BASE_URL}/studentSignup`; + // const apiUrl = `http://localhost:3000/api/v1/studentSignUp`; + try { setLoading(true); const response = await axios.post(apiUrl, formData); @@ -98,16 +96,17 @@ function Signup() { toast.success("Account Created Successfully!"); navigate("/home"); } catch (error) { - const errorMessage = error.response?.data?.message || "Failed to login. Please try again."; - toast.error(errorMessage); - console.log("This is our error ", error); + const errorMessage = + error.response?.data?.message || + "Failed to login. Please try again."; + toast.error(errorMessage); + console.log("This is our error ", error); } finally { setLoading(false); } } else { - - const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup` - // const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`; + const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`; + // const apiUrl = `${process.env.REACT_APP_BASE_URL}/canteenSignup`; try { setLoading(true); @@ -128,7 +127,6 @@ function Signup() { } } else { toast.error("Password must pass all the criteria"); - } } @@ -239,6 +237,27 @@ function Signup() { +
+ + setshowconfirmPassword((prev) => !prev)} + > + {showconfirmPassword ? ( + + ) : ( + + )} + +