From b24d52256c961fbb352f6897c07e155fdb935104 Mon Sep 17 00:00:00 2001 From: tamalCodes Date: Mon, 11 Mar 2024 14:42:38 +0530 Subject: [PATCH] fix: validations refactored --- config/passport-googleAuth-strategy.js | 2 +- index.js | 3 ++ package-lock.json | 54 ++++++++++++++++++++++---- package.json | 1 + routes/club/Club.js | 8 ++-- routes/display/Display.js | 2 +- routes/events/Event.js | 7 +--- routes/user/Auth.js | 32 +++++++-------- routes/user/User.js | 12 +++--- schema/club/ClubSchema.js | 2 +- schema/user/ReportProblemSchema.js | 4 +- schema/user/UserSchema.js | 8 ++-- 12 files changed, 86 insertions(+), 49 deletions(-) diff --git a/config/passport-googleAuth-strategy.js b/config/passport-googleAuth-strategy.js index 8800f6e..8278a13 100644 --- a/config/passport-googleAuth-strategy.js +++ b/config/passport-googleAuth-strategy.js @@ -22,7 +22,7 @@ passport.use( user = await User.create({ name: profile.displayName, email: profile.emails[0].value, - username: profile.emails[0].value.split("@")[0], + userName: profile.emails[0].value.split("@")[0], password: crypto.randomBytes(20).toString("hex"), }); diff --git a/index.js b/index.js index ed0578c..454b83b 100644 --- a/index.js +++ b/index.js @@ -8,12 +8,15 @@ const passport = require("passport"); require("./config/passport-googleAuth-strategy"); const path = require("path"); +const bodyParser = require("body-parser"); let port = process.env.PORT || 5000; const app = express(); dotenv.config(); connectToMongo(); +app.use(bodyParser.json({ limit: "10mb" })); + app.use( cors({ origin: (origin, callback) => { diff --git a/package-lock.json b/package-lock.json index d798fd6..a094e27 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@geoapify/geocoder-autocomplete": "^1.5.1", "@geoapify/react-geocoder-autocomplete": "^1.5.0", "bcryptjs": "^2.4.3", + "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", "crypto": "^1.0.1", @@ -2534,12 +2535,12 @@ "integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==" }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -2547,7 +2548,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -3992,6 +3993,29 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, "node_modules/express/node_modules/cookie": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", @@ -4013,6 +4037,20 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -10549,9 +10587,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index 0cdece5..f4154a5 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "@geoapify/geocoder-autocomplete": "^1.5.1", "@geoapify/react-geocoder-autocomplete": "^1.5.0", "bcryptjs": "^2.4.3", + "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", "crypto": "^1.0.1", diff --git a/routes/club/Club.js b/routes/club/Club.js index 8847282..34a325c 100644 --- a/routes/club/Club.js +++ b/routes/club/Club.js @@ -5,10 +5,10 @@ const router = express.Router(); router.get("/", async (req, res) => { try { - const { username } = req.query; + const { userName } = req.query; - if (username) { - const clubdetails = await User.findOne({ username }); + if (userName) { + const clubdetails = await User.findOne({ userName }); if (!clubdetails) return res .status(STATUSCODE.NOT_FOUND) @@ -17,7 +17,7 @@ router.get("/", async (req, res) => { } const clubs = await User.find({ - usertype: "club", + userType: "club", }); res.json(clubs); diff --git a/routes/display/Display.js b/routes/display/Display.js index 8b31495..ff2fb4f 100644 --- a/routes/display/Display.js +++ b/routes/display/Display.js @@ -20,7 +20,7 @@ router.get("/users", async (req, res) => { // * Route 2 - Show all available Clubs in the DB router.get("/clubs", async (req, res) => { try { - const allClubs = await User.find({ usertype: "club" }); + const allClubs = await User.find({ userType: "club" }); res.json(allClubs); } catch (error) { res diff --git a/routes/events/Event.js b/routes/events/Event.js index c39912f..5ef09bc 100644 --- a/routes/events/Event.js +++ b/routes/events/Event.js @@ -38,17 +38,12 @@ router.post("/create", async (req, res) => { !data.description || !data.coverImage || !data.mode || - !data.startDate || - !data.endDate || - !data.startTime || - !data.endTime || !data.city || !data.state || !data.country || !data.address || !data.mapIframe ) { - console.log(data); return res .status(STATUSCODE.BAD_REQUEST) .json({ message: "Missing Required Fields" }); @@ -71,7 +66,7 @@ router.post("/create", async (req, res) => { ...data, uid, hostName: user.name, - hostUsername: user.username, + hostUsername: user.userName, createdAt: setTime(), updatedAt: setTime(), }); diff --git a/routes/user/Auth.js b/routes/user/Auth.js index 04e12f1..9061d53 100644 --- a/routes/user/Auth.js +++ b/routes/user/Auth.js @@ -36,11 +36,11 @@ router.post("/signup", async (req, res) => { } const hashedPassword = await bcrypt.hash(data.password, 10); - const username = email.split("@")[0]; + const userName = email.split("@")[0]; const newUser = new User({ ...data, - username, + userName, email, password: hashedPassword, }); @@ -55,9 +55,9 @@ router.post("/signup", async (req, res) => { res .status(STATUSCODE.CREATED) .cookie("Token", token, defaultCookie) - .cookie("username", username, frontendCookie) + .cookie("userName", userName, frontendCookie) .cookie("isLoggedIn", true, frontendCookie) - .cookie("usertype", data?.usertype, frontendCookie) + .cookie("userType", data?.userType, frontendCookie) .json({ message: STATUSMESSAGE.SIGNUP_SUCCESS, user, @@ -99,9 +99,9 @@ router.post("/signin", async (req, res) => { res .status(STATUSCODE.CREATED) .cookie("Token", token, defaultCookie) - .cookie("username", existingUser.username, frontendCookie) + .cookie("userName", existingUser.userName, frontendCookie) .cookie("isLoggedIn", true, frontendCookie) - .cookie("usertype", existingUser.usertype, frontendCookie) + .cookie("userType", existingUser.userType, frontendCookie) .json({ message: STATUSMESSAGE.LOGIN_SUCCESS, user, @@ -145,8 +145,8 @@ router.post("/update", async (req, res) => { // Updated User const UserData = { - firstname: existingUser.firstname, - lastname: existingUser.lastname, + firstName: existingUser.firstName, + lastName: existingUser.lastName, email: email, password: newHashedPassword, address: existingUser.address, @@ -171,7 +171,7 @@ router.get("/google", (req, res) => { redirect_uri: process.env.CALLBACK_URL, scope: "profile email ", client_id: process.env.CLIENT_ID, - state: req.query.usertype, + state: req.query.userType, }); const redirectURL = `${googleAuthURL}?${params}`; @@ -186,17 +186,17 @@ router.get( failureRedirect: "auth/login/failed", }), async (req, res) => { - const usertype = req.query.state; + const userType = req.query.state; if (req.isAuthenticated()) { const user = req.user; try { const existingUser = await User.findOne({ email: user.email }); if (!existingUser) { - // This is a new account, update usertype + // This is a new account, update userType await User.create({ email: user.email, - usertype: usertype, + userType: userType, }); } res @@ -246,9 +246,9 @@ router.get("/login/success", (req, res) => { domain: process.env.ORIGIN_DOMAIN, }) .cookie("Token", token, defaultCookie) - .cookie("username", req.user.username, frontendCookie) + .cookie("userName", req.user.userName, frontendCookie) .cookie("isLoggedIn", true, frontendCookie) - .cookie("usertype", "user", frontendCookie) + .cookie("userType", "user", frontendCookie) .json({ message: STATUSMESSAGE.LOGIN_SUCCESS, user, @@ -270,7 +270,7 @@ router.get("/logout", (req, res) => { sameSite: "none", domain: process.env.ORIGIN_DOMAIN, }) - .cookie("username", false, { + .cookie("userName", false, { expires: new Date(0), httpOnly: false, secure: true, @@ -284,7 +284,7 @@ router.get("/logout", (req, res) => { sameSite: "none", domain: process.env.ORIGIN_DOMAIN, }) - .cookie("usertype", false, { + .cookie("userType", false, { expires: new Date(0), httpOnly: false, secure: true, diff --git a/routes/user/User.js b/routes/user/User.js index fa16a9f..2eeeaa0 100644 --- a/routes/user/User.js +++ b/routes/user/User.js @@ -7,10 +7,10 @@ const jwt = require("jsonwebtoken"); router.get("/", async (req, res) => { try { - const { username } = req.query; + const { userName } = req.query; - if (username) { - const userdetails = await User.findOne({ username }); + if (userName) { + const userdetails = await User.findOne({ userName }); if (!userdetails) return res @@ -21,7 +21,7 @@ router.get("/", async (req, res) => { } const users = await User.find({ - usertype: "individual", + userType: "individual", }); res.json(users); @@ -96,8 +96,8 @@ router.post("/report", async (req, res) => { const data = req.body; const ReportData = ReportProblem({ - firstname: data.firstname, - lastname: data.lastname, + firstName: data.firstName, + lastName: data.lastName, email: data.email, reportmessage: data.reportmessage, }); diff --git a/schema/club/ClubSchema.js b/schema/club/ClubSchema.js index 6dd1b46..9318286 100644 --- a/schema/club/ClubSchema.js +++ b/schema/club/ClubSchema.js @@ -41,7 +41,7 @@ const ClubsSchema = mongoose.Schema({ type: String, required: true, }, - username: { + userName: { type: String, required: true, }, diff --git a/schema/user/ReportProblemSchema.js b/schema/user/ReportProblemSchema.js index 65ce7ce..62389b7 100644 --- a/schema/user/ReportProblemSchema.js +++ b/schema/user/ReportProblemSchema.js @@ -2,11 +2,11 @@ const mongoose = require("mongoose"); const ReportProblemSchema = mongoose.Schema( { - firstname: { + firstName: { type: String, required: true, }, - lastname: { + lastName: { type: String, required: true, }, diff --git a/schema/user/UserSchema.js b/schema/user/UserSchema.js index 507be97..23f3e55 100644 --- a/schema/user/UserSchema.js +++ b/schema/user/UserSchema.js @@ -1,14 +1,14 @@ const mongoose = require("mongoose"); const UserSchema = mongoose.Schema({ - usertype: { type: String }, - username: { + userType: { type: String }, + userName: { type: String, required: true, }, name: { type: String }, - firstname: { type: String }, - lastname: { type: String }, + firstName: { type: String }, + lastName: { type: String }, email: { type: String, required: true,