From 36801e70da0dde5baa7241e945c9bbc80243eabc Mon Sep 17 00:00:00 2001 From: Maheen Fatma Date: Wed, 21 May 2025 15:07:10 +0530 Subject: [PATCH 1/3] signup controller issues resolved --- client/src/componets/Login.js | 1 + server/controller/user-contoller.js | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client/src/componets/Login.js b/client/src/componets/Login.js index 8f06606b..2ecbbf67 100644 --- a/client/src/componets/Login.js +++ b/client/src/componets/Login.js @@ -27,6 +27,7 @@ const Login = () => { useEffect(() => { setIsSignup(isSignupButtonPressed); }, [isSignupButtonPressed]); + const sendRequest = async (type = "login") => { console.log("inside send req"); console.log(`${config.BASE_URL}/api/users/${type}`); diff --git a/server/controller/user-contoller.js b/server/controller/user-contoller.js index 192c7d00..657cb12c 100644 --- a/server/controller/user-contoller.js +++ b/server/controller/user-contoller.js @@ -25,7 +25,8 @@ const signUp = async(req,res,next) =>{ try{ existingUser = await User.findOne({email}) }catch(e){ - console.log(err); + console.log(e); + return res.status(500).json({ message: "Error checking existing user" }); } if(existingUser){ @@ -39,7 +40,7 @@ const signUp = async(req,res,next) =>{ }); try{ - user.save(); + await user.save(); return res.status(201).json({ user }) } catch(e){console.log(e);} From 18d516e4de0936498d5d65b36202a86273309da4 Mon Sep 17 00:00:00 2001 From: Maheen Fatma Date: Wed, 21 May 2025 15:47:28 +0530 Subject: [PATCH 2/3] fix: Hashed the password in the frontend as well --- client/package.json | 1 + client/src/componets/Login.js | 33 +++++++++++++++++------------ server/config/db.js | 2 ++ server/controller/user-contoller.js | 10 ++++++--- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/client/package.json b/client/package.json index 5be513f5..30d23086 100644 --- a/client/package.json +++ b/client/package.json @@ -13,6 +13,7 @@ "@testing-library/react": "^13.2.0", "@testing-library/user-event": "^13.5.0", "axios": "^0.27.2", + "bcryptjs": "^3.0.2", "react": "^18.1.0", "react-dom": "^18.1.0", "react-redux": "^8.0.2", diff --git a/client/src/componets/Login.js b/client/src/componets/Login.js index 2ecbbf67..94ed2744 100644 --- a/client/src/componets/Login.js +++ b/client/src/componets/Login.js @@ -1,6 +1,7 @@ import { Box, Button, TextField, Typography } from "@mui/material"; import React, { useEffect, useState } from "react"; import axios from "axios"; +import bcrypt from 'bcryptjs'; import { useDispatch } from "react-redux"; import { authActions } from "../store"; import { useNavigate, useLocation } from "react-router-dom"; @@ -27,18 +28,18 @@ const Login = () => { useEffect(() => { setIsSignup(isSignupButtonPressed); }, [isSignupButtonPressed]); - - const sendRequest = async (type = "login") => { + + const sendRequest = async (type = "login",dataToSend) => { console.log("inside send req"); console.log(`${config.BASE_URL}/api/users/${type}`); - const res = await axios - .post(`${config.BASE_URL}/api/users/${type}`, { - name: inputs.name, - email: inputs.email, - password: inputs.password, - }) - .catch((err) => console.log(err)); - + let res; + try { + res = await axios.post(`${config.BASE_URL}/api/users/${type}`, dataToSend) + } catch (err) { + console.error("Signup error:", err); + return; + } + const data = await res.data; console.log("return"); console.log(data); @@ -47,14 +48,20 @@ const Login = () => { const handleSubmit = (e) => { e.preventDefault(); - console.log(inputs); + const salt = bcrypt.genSaltSync(10); + const hashedPassword = bcrypt.hashSync(inputs.password, salt); + const dataToSend = { + ...inputs, + password: hashedPassword, + }; + console.log(dataToSend); if (isSignup) { - sendRequest("signup") + sendRequest("signup", dataToSend) .then((data) => localStorage.setItem("userId", data.user._id)) .then(() => dispath(authActions.login())) .then(() => naviagte("/blogs")); } else { - sendRequest() + sendRequest("login", dataToSend) .then((data) => localStorage.setItem("userId", data.user._id)) .then(() => dispath(authActions.login())) .then(() => naviagte("/blogs")); diff --git a/server/config/db.js b/server/config/db.js index cd0e9cdd..34ec8195 100644 --- a/server/config/db.js +++ b/server/config/db.js @@ -6,5 +6,7 @@ mongoose.set('strictQuery', false); mongoose.connect("mongodb://127.0.0.1:27017/BlogApp").then(()=>{ console.log("connected!"); }).catch((err)=>{ + console.log("error in mongodb connection"); + console.log(err); }) \ No newline at end of file diff --git a/server/controller/user-contoller.js b/server/controller/user-contoller.js index 657cb12c..5ebcfa5a 100644 --- a/server/controller/user-contoller.js +++ b/server/controller/user-contoller.js @@ -18,6 +18,7 @@ const getAllUser = async(req,res,next) =>{ } const signUp = async(req,res,next) =>{ + console.log("Received signup data:", req.body); const { name , email , password } = req.body; let existingUser; @@ -28,11 +29,12 @@ const signUp = async(req,res,next) =>{ console.log(e); return res.status(500).json({ message: "Error checking existing user" }); } - - if(existingUser){ + + if(existingUser){ console.log("here"); return res.status(400).json({message : "User is already exists!"}) } const hashedPassword = bcrypt.hashSync(password); + const user = new User({ name,email, password: hashedPassword, @@ -43,7 +45,9 @@ const signUp = async(req,res,next) =>{ await user.save(); return res.status(201).json({ user }) } - catch(e){console.log(e);} + catch(e){console.log(e); + return res.status(500).json({ message: "Signup failed!" }); + } } const logIn = async(req,res,next) => { From bf9be51ea16ccfd497237df5d9d770e36094ea42 Mon Sep 17 00:00:00 2001 From: Maheen Fatma Date: Thu, 29 May 2025 00:29:58 +0530 Subject: [PATCH 3/3] Remove unnecessary log --- client/src/componets/Login.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/client/src/componets/Login.js b/client/src/componets/Login.js index 94ed2744..031b06c9 100644 --- a/client/src/componets/Login.js +++ b/client/src/componets/Login.js @@ -30,8 +30,7 @@ const Login = () => { }, [isSignupButtonPressed]); const sendRequest = async (type = "login",dataToSend) => { - console.log("inside send req"); - console.log(`${config.BASE_URL}/api/users/${type}`); + let res; try { res = await axios.post(`${config.BASE_URL}/api/users/${type}`, dataToSend) @@ -41,8 +40,6 @@ const Login = () => { } const data = await res.data; - console.log("return"); - console.log(data); return data; }; @@ -54,7 +51,6 @@ const Login = () => { ...inputs, password: hashedPassword, }; - console.log(dataToSend); if (isSignup) { sendRequest("signup", dataToSend) .then((data) => localStorage.setItem("userId", data.user._id))