From c5a847b9c8e4f2aad92c8b2cb73003ca67cf8167 Mon Sep 17 00:00:00 2001 From: Mayank Date: Sun, 28 Jun 2026 15:46:28 +0530 Subject: [PATCH] Removed password on console --- client/src/componets/Blogs.js | 16 +++-- client/src/componets/Login.js | 25 ++++--- server/.env | 1 + server/.env.example | 1 - server/controller/user-contoller.js | 102 +++++++++++++++----------- server/package.json | 106 ++++++---------------------- server/yarn.lock | 10 +++ 7 files changed, 119 insertions(+), 142 deletions(-) create mode 100644 server/.env delete mode 100644 server/.env.example diff --git a/client/src/componets/Blogs.js b/client/src/componets/Blogs.js index 959d98cb..e2c0e7a7 100644 --- a/client/src/componets/Blogs.js +++ b/client/src/componets/Blogs.js @@ -6,14 +6,20 @@ import config from "../config"; const Blogs = () => { const [blogs, setBlogs] = useState(); const sendRequest = async () => { - const res = await axios - .get(`${config.BASE_URL}/api/blogs`) - .catch((err) => console.log(err)); - const data = await res.data; + let res; + try { + res = await axios.get(`${config.BASE_URL}/api/blogs`); + } catch (err) { + console.log(err); + throw err; // Re-throw the error to be caught by the .then().catch() chain + } + const data = res.data; return data; }; useEffect(() => { - sendRequest().then((data) => setBlogs(data.blogs)); + sendRequest() + .then((data) => setBlogs(data.blogs)) + .catch((err) => console.log("Fetch blogs error:", err)); // Add catch block }, []); console.log(blogs); return ( diff --git a/client/src/componets/Login.js b/client/src/componets/Login.js index 8f06606b..2679c076 100644 --- a/client/src/componets/Login.js +++ b/client/src/componets/Login.js @@ -30,15 +30,19 @@ const Login = () => { const sendRequest = async (type = "login") => { console.log("inside send req"); console.log(`${config.BASE_URL}/api/users/${type}`); - const res = await axios - .post(`${config.BASE_URL}/api/users/${type}`, { + let res; + try { + res = await axios.post(`${config.BASE_URL}/api/users/${type}`, { name: inputs.name, email: inputs.email, password: inputs.password, - }) - .catch((err) => console.log(err)); + }); + } catch (err) { + console.log(err); + throw err; // Re-throw the error to be caught by the .then().catch() chain + } - const data = await res.data; + const data = res.data; console.log("return"); console.log(data); return data; @@ -46,17 +50,18 @@ const Login = () => { const handleSubmit = (e) => { e.preventDefault(); - console.log(inputs); if (isSignup) { sendRequest("signup") - .then((data) => localStorage.setItem("userId", data.user._id)) + .then((data) => localStorage.setItem("userId", data.data.user._id)) .then(() => dispath(authActions.login())) - .then(() => naviagte("/blogs")); + .then(() => naviagte("/blogs")) + .catch((err) => console.log("Signup error:", err)); // Add catch block } else { sendRequest() - .then((data) => localStorage.setItem("userId", data.user._id)) + .then((data) => localStorage.setItem("userId", data.data.user._id)) .then(() => dispath(authActions.login())) - .then(() => naviagte("/blogs")); + .then(() => naviagte("/blogs")) + .catch((err) => console.log("Login error:", err)); // Add catch block } }; return ( diff --git a/server/.env b/server/.env new file mode 100644 index 00000000..86ef24c5 --- /dev/null +++ b/server/.env @@ -0,0 +1 @@ +MONGO_URI = mongodb://localhost:27017/BlogApp \ No newline at end of file diff --git a/server/.env.example b/server/.env.example deleted file mode 100644 index 4522065b..00000000 --- a/server/.env.example +++ /dev/null @@ -1 +0,0 @@ -MONGO_URI = mongodb://127.0.0.1:27017/BlogApp \ No newline at end of file diff --git a/server/controller/user-contoller.js b/server/controller/user-contoller.js index c6387a7d..a32b18b9 100644 --- a/server/controller/user-contoller.js +++ b/server/controller/user-contoller.js @@ -4,62 +4,78 @@ const { ApiResponse } = require("../utils/ApiResponse"); const { ApiError } = require("../utils/ApiError"); const getAllUser = async (req, res, next) => { - try { - const users = await User.find(); - if (!users || users.length === 0) { - return res.status(404).json(new ApiError(404, "Users not found")); - } - return res.status(200).json(new ApiResponse(200, { users }, "Users fetched successfully")); - } catch (err) { - console.error(err); - return res.status(500).json(new ApiError(500, "Server error while fetching users")); + try { + const users = await User.find(); + if (!users || users.length === 0) { + return res.status(404).json(new ApiError(404, "Users not found")); } + return res + .status(200) + .json(new ApiResponse(200, { users }, "Users fetched successfully")); + } catch (err) { + console.error(err); + return res + .status(500) + .json(new ApiError(500, "Server error while fetching users")); + } }; const signUp = async (req, res, next) => { - const { name, email, password } = req.body; + + const { name, email, password } = req.body; - try { - const existingUser = await User.findOne({ email }); - if (existingUser) { - return res.status(400).json(new ApiError(400, "User already exists")); - } + try { + const existingUser = await User.findOne({ email }); + if (existingUser) { + return res.status(400).json(new ApiError(400, "User already exists")); + } - const hashedPassword = bcrypt.hashSync(password, 10); - const user = new User({ - name, - email, - password: hashedPassword, - blogs: [] - }); + const hashedPassword = bcrypt.hashSync(password, 10); + const user = new User({ + name, + email, + password: hashedPassword, + blogs: [], + }); - await user.save(); - return res.status(201).json(new ApiResponse(201, { user }, "User registered successfully")); - } catch (e) { - console.error(e); - return res.status(500).json(new ApiError(500, "Server error while signing up")); - } + await user.save(); + return res + .status(201) + .json(new ApiResponse(201, { user }, "User registered successfully")); + } catch (e) { + console.error(e); + return res + .status(500) + .json(new ApiError(500, "Server error while signing up")); + } }; const logIn = async (req, res, next) => { - const { email, password } = req.body; - - try { - const existingUser = await User.findOne({ email }); - if (!existingUser) { - return res.status(404).json(new ApiError(404, "User not found")); - } + const { email, password } = req.body; - const isPasswordCorrect = bcrypt.compareSync(password, existingUser.password); - if (!isPasswordCorrect) { - return res.status(400).json(new ApiError(400, "Incorrect Password")); - } + try { + const existingUser = await User.findOne({ email }); + if (!existingUser) { + return res.status(404).json(new ApiError(404, "User not found")); + } - return res.status(200).json(new ApiResponse(200, { user: existingUser }, "Login successful")); - } catch (err) { - console.error(err); - return res.status(500).json(new ApiError(500, "Server error while logging in")); + const isPasswordCorrect = bcrypt.compareSync( + password, + existingUser.password, + ); + if (!isPasswordCorrect) { + return res.status(400).json(new ApiError(400, "Incorrect Password")); } + + return res + .status(200) + .json(new ApiResponse(200, { user: existingUser }, "Login successful")); + } catch (err) { + console.error(err); + return res + .status(500) + .json(new ApiError(500, "Server error while logging in")); + } }; module.exports = { getAllUser, signUp, logIn }; diff --git a/server/package.json b/server/package.json index 0bc1d46f..f3e70650 100644 --- a/server/package.json +++ b/server/package.json @@ -1,83 +1,23 @@ -{ - - - "name": "blogapp", - - - "version": "1.0.0", - - - "description": "", - - - "main": "server.js", - - - "scripts": { - - - - - "start": "nodemon server.js", - - - - - "test": "echo \"Error: no test specified\" && exit 1" - - - }, - - - "author": "khushi patel", - - - "license": "ISC", - - - "devDependencies": { - - - - - "nodemon": "^2.0.16" - - - }, - - - "dependencies": { - - - - - "bcryptjs": "^2.4.3", - - - - - "cors": "^2.8.5", - - - - - "dotenv": "^16.5.0", - - - - - "express": "^4.18.1", - - - - - "helmet": "^8.1.0", - - - - - "mongoose": "^6.3.4" - - - } -} +{ + "name": "blogapp", + "version": "1.0.0", + "description": "", + "main": "server.js", + "scripts": { + "start": "nodemon server.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "khushi patel", + "license": "ISC", + "devDependencies": { + "nodemon": "^2.0.16" + }, + "dependencies": { + "bcryptjs": "^2.4.3", + "cors": "^2.8.5", + "dotenv": "^16.5.0", + "express": "^4.18.1", + "helmet": "^8.1.0", + "mongoose": "^6.3.4" + } +} diff --git a/server/yarn.lock b/server/yarn.lock index 6a549298..272be7b2 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -1213,6 +1213,11 @@ fresh@0.5.2: resolved "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz" integrity sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q== +fsevents@~2.3.2: + version "2.3.3" + resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz" + integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw== + function-bind@^1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz" @@ -1272,6 +1277,11 @@ hasown@^2.0.0: dependencies: function-bind "^1.1.2" +helmet@^8.1.0: + version "8.2.0" + resolved "https://registry.npmjs.org/helmet/-/helmet-8.2.0.tgz" + integrity sha512-DRgTIUgnWcJ62KyarxxziuqYxKGnR6Rgg19BlbucN/dpmJbl1XOit6qvoOX0ZT+HhWe5OUVhU/a1zpGyc1xA0Q== + http-errors@2.0.0: version "2.0.0" resolved "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz"