From 62efee242dd73ce2708537d3040de899d00234b4 Mon Sep 17 00:00:00 2001 From: dhruveeldoshi Date: Sat, 15 May 2021 02:47:43 -0400 Subject: [PATCH] bugs fix 2 --- public/js/script.js | 20 ++++-- public/js/signUp.js | 1 - routes/products.js | 78 ++++++++++++----------- routes/users.js | 94 ++++++++++++++-------------- views/pages/singleProduct.handlebars | 9 ++- 5 files changed, 107 insertions(+), 95 deletions(-) diff --git a/public/js/script.js b/public/js/script.js index 617e5b6..928cfa6 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -134,15 +134,18 @@ $(document).ready(function () { $(".buy_now").on("click", function (e) { var id = $(this).attr("data-id"); - alert("Product has beed added to Cart"); + $.ajax({ url: "/addtocart/" + id, // url where to submit the request type: "patch", // type of action POST || GET success: function (data) { + alert("Product has beed added to Cart"); window.location.href = "http://localhost:3000/products/product/" + id; }, - error: function () {}, + error: function () { + window.location.href = "/users/form"; + }, }); }); @@ -168,7 +171,6 @@ $(document).ready(function () { }, error: function () {}, }); - }); $(".add_review").on("click", function (e) { @@ -184,7 +186,9 @@ $(document).ready(function () { success: function (data) { window.location.href = "http://localhost:3000/products/product/" + id; }, - error: function () {}, + error: function () { + window.location.href = "/users/form"; + }, }); }); @@ -201,7 +205,9 @@ $(document).ready(function () { $("#unliked").attr("id", "liked"); userLiked = false; }, - error: function () {}, + error: function () { + window.location.href = "/users/form"; + }, }); } else { $.ajax({ @@ -212,7 +218,9 @@ $(document).ready(function () { $("#liked").attr("id", "unliked"); userLiked = true; }, - error: function () {}, + error: function () { + window.location.href = "/users/form"; + }, }); } }); diff --git a/public/js/signUp.js b/public/js/signUp.js index d4599b2..4c4a124 100644 --- a/public/js/signUp.js +++ b/public/js/signUp.js @@ -64,7 +64,6 @@ if (!validString(info.zipCode)) zipCodeInput.addClass("is-invalid"); if (!hasErrors) { - alert("in !if"); signupForm.unbind().submit(); } else { submitInfo.prop("disabled", false); diff --git a/routes/products.js b/routes/products.js index 8a41182..39b634e 100644 --- a/routes/products.js +++ b/routes/products.js @@ -98,41 +98,40 @@ router.get("/", async (req, res) => { //to get product by Id provided router.get("/products/product/:id", async (req, res) => { try { - // if (req.session.user) { - errorHandler.checkStringObjectId(req.params.id, "Product ID"); - let product = await productsData.getProductById(req.params.id); - const productComments = await productsData.getProductComments( - req.params.id - ); - - const commentList = []; - for (comment of productComments) { - commentList.push(comment.commentText); - } + if (req.session.user) { + errorHandler.checkStringObjectId(req.params.id, "Product ID"); + let product = await productsData.getProductById(req.params.id); + const productComments = await productsData.getProductComments( + req.params.id + ); - await usersData.userViewsAProduct( - "609a2fca59ef0ecfeb7b57af", - req.params.id - ); + const commentList = []; + for (comment of productComments) { + commentList.push(comment.commentText); + } - let hascomments = true; + await usersData.userViewsAProduct( + "609a2fca59ef0ecfeb7b57af", + req.params.id + ); - if (commentList.length == 0) { - hascomments = false; - } + let hascomments = true; - res.render("pages/singleProduct", { - authenticated: req.session.user ? true : false, - adminAuth: req.session.admin ? true : false, - title: product.title, - product: product, - comments: commentList, - hascomments: hascomments, - }); + if (commentList.length == 0) { + hascomments = false; + } - // } else { - // res.sendStatus(404).json({ message: "User not Authenticated" }); - // } + res.render("pages/singleProduct", { + authenticated: req.session.user ? true : false, + adminAuth: req.session.admin ? true : false, + title: product.title, + product: product, + comments: commentList, + hascomments: hascomments, + }); + } else { + return res.sendStatus(404).json({ message: "User not Authenticated" }); + } } catch (e) { return res.status(404).json({ error: "product not found" }); } @@ -250,7 +249,7 @@ router.get("/buy", async (req, res) => { }); router.patch("/product/comment/:id", async (req, res) => { - const comment_text = req.body.review; + const comment_text = xss(req.body.review); try { errorHandler.checkStringObjectId(req.params.id, "Product ID"); errorHandler.checkString(comment_text); @@ -260,29 +259,32 @@ router.patch("/product/comment/:id", async (req, res) => { req.params.id, comment_text ); - res.sendStatus(200); + return res.sendStatus(200); } else { - res.sendStatus(404); + return res.sendStatus(404); } } catch (error) { console.log(error); - res.sendStatus(404); + return res.sendStatus(404); } }); router.patch("/addtocart/:id", async (req, res) => { + console.log("edws"); + // if (req.session.user == "undefined") { + // res.redirect("/"); + // } try { errorHandler.checkStringObjectId(req.params.id, "Product ID"); if (req.session.user) { - console.log(req.session); req.session.cartItems.push(req.params.id); - res.sendStatus(200); + return res.sendStatus(200); } else { - res.sendStatus(404); + return res.sendStatus(404); } } catch (error) { console.log(error); - res.sendStatus(404); + return res.sendStatus(404); } }); //////////////// diff --git a/routes/users.js b/routes/users.js index db24409..09602cc 100644 --- a/routes/users.js +++ b/routes/users.js @@ -167,7 +167,7 @@ router.post("/login", async (req, res) => { req.session.cartItems = []; return res.redirect("/users/details"); - return res.redirect("/"); + // return res.redirect("/"); } else { errors.push("User E-mail address or password does not match"); return res.render("pages/loginPage", { @@ -192,50 +192,51 @@ router.get("/signup", async (req, res) => { }); router.post("/signup", async (req, res) => { - dataSignIn = req.body; - const firstName = xss(dataSignIn.firstName); - const lastName = xss(dataSignIn.lastName); - const email = xss(dataSignIn.emailId); - const password = xss(dataSignIn.password); - const phoneNumber = xss(dataSignIn.phoneNumber); - const Line1 = xss(dataSignIn.Line1); - const Line2 = xss(dataSignIn.Line2); - const City = xss(dataSignIn.City); - const State = xss(dataSignIn.State); - const ZipCode = xss(dataSignIn.ZipCode); - - errors = []; - if (!errorCheck.stringCheck(firstName)) - errors.push("Invalid FirstName (routes/users)"); - if (!errorCheck.stringCheck(lastName)) - errors.push("Invalid LastName (routes/users)"); - if (!errorCheck.emailValidate(email)) - errors.push("Invalid Email (routes/users)"); - if (!errorCheck.validPassword(password)) - errors.push("Invalid Password (routes/users)"); - if (!errorCheck.phoneNumberValid(phoneNumber)) - errors.push("Invalid PhoneNumber (routes/users)"); - if (!errorCheck.stringCheck(Line1)) - errors.push("Invalid LineOne (routes/users )"); - - if (!errorCheck.stringCheck(Line2)) - errors.push("Invalid LineTwo (routes/users )"); - if (!errorCheck.stringCheck(City)) - errors.push("Invalid City (routes/users )"); - - if (!errorCheck.stringCheck(State)) - errors.push("Invalid State routes/users )"); - if (!errorCheck.zipcCodeValid(ZipCode)) - errors.push("Invalid Zip Code (routes/users )"); - address = { - Line1: Line1, - Line2: Line2, - City: City, - State: State, - ZipCode: parseInt(ZipCode), - Country: "USA", - }; try { + dataSignIn = req.body; + const firstName = xss(dataSignIn.firstName); + const lastName = xss(dataSignIn.lastName); + const email = xss(dataSignIn.emailId); + const password = xss(dataSignIn.password); + const phoneNumber = xss(dataSignIn.phoneNumber); + const Line1 = xss(dataSignIn.Line1); + const Line2 = xss(dataSignIn.Line2); + const City = xss(dataSignIn.City); + const State = xss(dataSignIn.State); + const ZipCode = xss(dataSignIn.ZipCode); + + errors = []; + if (!errorCheck.stringCheck(firstName)) + errors.push("Invalid FirstName (routes/users)"); + if (!errorCheck.stringCheck(lastName)) + errors.push("Invalid LastName (routes/users)"); + if (!errorCheck.emailValidate(email)) + errors.push("Invalid Email (routes/users)"); + if (!errorCheck.validPassword(password)) + errors.push("Invalid Password (routes/users)"); + if (!errorCheck.phoneNumberValid(phoneNumber)) + errors.push("Invalid PhoneNumber (routes/users)"); + if (!errorCheck.stringCheck(Line1)) + errors.push("Invalid LineOne (routes/users )"); + + if (!errorCheck.stringCheck(Line2)) + errors.push("Invalid LineTwo (routes/users )"); + if (!errorCheck.stringCheck(City)) + errors.push("Invalid City (routes/users )"); + + if (!errorCheck.stringCheck(State)) + errors.push("Invalid State routes/users )"); + if (!errorCheck.zipcCodeValid(ZipCode)) + errors.push("Invalid Zip Code (routes/users )"); + address = { + Line1: Line1, + Line2: Line2, + City: City, + State: State, + ZipCode: parseInt(ZipCode), + Country: "USA", + }; + dataError.checkAddress(address); // Just for woking part I'm throwing JSON error if (errors.length > 0) { @@ -248,14 +249,11 @@ router.post("/signup", async (req, res) => { } const allUsers = await usersData.getAllUsers(); - let emailUsed; + let emailUsed = false; allUsers.find((user) => { if (user.emailId === email.toLowerCase()) { emailUsed = true; - return emailUsed; - } else { - emailUsed = false; return emailUsed; } }); diff --git a/views/pages/singleProduct.handlebars b/views/pages/singleProduct.handlebars index 69cf267..bc97114 100644 --- a/views/pages/singleProduct.handlebars +++ b/views/pages/singleProduct.handlebars @@ -19,7 +19,7 @@ @@ -81,7 +81,12 @@
- single product + single product