From 5e84775685e21b126b0e330e883ac60814538149 Mon Sep 17 00:00:00 2001 From: TJSidhu1 Date: Thu, 20 Apr 2023 11:11:40 -0700 Subject: [PATCH 1/6] Updated index.js, feed.js for json response bypassing the authentication in order to test the changes --- backend/src/config/auth.js | 11 ++++++----- backend/src/routes/feed.js | 5 +++-- backend/src/routes/index.js | 10 +++------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/backend/src/config/auth.js b/backend/src/config/auth.js index 8b645f2d..d1301141 100644 --- a/backend/src/config/auth.js +++ b/backend/src/config/auth.js @@ -1,9 +1,10 @@ module.exports ={ ensureAuthenticated: function(req, res, next){ - if(req.isAuthenticated()){ - console.log(req.user.name); - return next(); - } - return res.redirect('/'); + // if(req.isAuthenticated()){ + // console.log(req.user.name); + // return next(); + // } + // return res.redirect('/'); + return next(); } } \ No newline at end of file diff --git a/backend/src/routes/feed.js b/backend/src/routes/feed.js index cb44c0c4..af4a1ea9 100644 --- a/backend/src/routes/feed.js +++ b/backend/src/routes/feed.js @@ -10,7 +10,8 @@ const sequelize = new Sequelize(require('../config/keys').PostgresURI); initModels(sequelize); router.get('/feed', ensureAuthenticated, function (req, res) { - if (!req.isAuthenticated()) { + // add ! back in the if statement + if (req.isAuthenticated()) { const errors = []; res.post('Unauthenticated'); } else { @@ -48,7 +49,7 @@ router.get('/feed', ensureAuthenticated, function (req, res) { if (allItems == null) { console.log("THIS IS ERROR " + allItems); } else { - res.json(JSON.stringify(allItems)); + res.json(allItems); } }); } diff --git a/backend/src/routes/index.js b/backend/src/routes/index.js index 617f1077..587d0f1c 100644 --- a/backend/src/routes/index.js +++ b/backend/src/routes/index.js @@ -14,7 +14,7 @@ initModels(sequelize); router.get('/', function(req, res, next) { if (!req.isAuthenticated()) { let errors = []; - res.render('index', { errors }); + res.json(errors); } else { res.redirect('/home', { title: 'Home' }); @@ -43,13 +43,9 @@ router.get('/home', ensureAuthenticated, function(req, res) { // } // console.log(ids); if (allItems.length > 0) { - res.render('dashboard', { - data: { name: req.user, allItems } - }) + res.json({ name: req.user, allItems }); } else { - res.render('dashboard', { - data: { name: req.user } - }) + res.json({ name: req.user }); } } } From 1d21a64314c47bc62c1589f2f428b99c3e7f1677 Mon Sep 17 00:00:00 2001 From: TJSidhu1 Date: Thu, 20 Apr 2023 11:27:43 -0700 Subject: [PATCH 2/6] Json response changes to the files index.js and feed.js along with changes to bypass authentication Fixes #130 --- backend/src/routes/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/routes/index.js b/backend/src/routes/index.js index 587d0f1c..5ee3c571 100644 --- a/backend/src/routes/index.js +++ b/backend/src/routes/index.js @@ -46,6 +46,7 @@ router.get('/home', ensureAuthenticated, function(req, res) { res.json({ name: req.user, allItems }); } else { res.json({ name: req.user }); + } } } From 9feae4d1da4c7e6023d281b803046ec82fbfc3da Mon Sep 17 00:00:00 2001 From: TJSidhu1 Date: Thu, 20 Apr 2023 11:37:02 -0700 Subject: [PATCH 3/6] Json response changes to the files index.js and feed.js along with changes to bypass authentication Fixes #130 --- backend/src/routes/feed.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/feed.js b/backend/src/routes/feed.js index af4a1ea9..13c9c604 100644 --- a/backend/src/routes/feed.js +++ b/backend/src/routes/feed.js @@ -49,7 +49,8 @@ router.get('/feed', ensureAuthenticated, function (req, res) { if (allItems == null) { console.log("THIS IS ERROR " + allItems); } else { - res.json(allItems); + res.json(allItems); + } }); } From 3e3a419a97d4074207cd4195b73e396ea820d831 Mon Sep 17 00:00:00 2001 From: shalavritha mamunooru Date: Sun, 23 Apr 2023 21:25:31 -0700 Subject: [PATCH 4/6] Fixes #130: Updated sale.js for json response --- backend/src/routes/sale.js | 73 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/backend/src/routes/sale.js b/backend/src/routes/sale.js index e9d8fccf..4075d266 100644 --- a/backend/src/routes/sale.js +++ b/backend/src/routes/sale.js @@ -29,8 +29,8 @@ router.get("/sale", ensureAuthenticated, function (req, res) { let errors = []; res.redirect("index", { errors }); } else { - res.render("sale", { - name: req.user.name, + res.json({ + name: req.user.name }); } }); @@ -73,9 +73,10 @@ router.get("/donor", ensureAuthenticated, function (req, res) { }, }) .then((allDonors) => { - res.render("donor", { - data: { name: req.user.name, donors: allDonors }, - }); + res.json({ + name: req.user.name, + donors: allDonors + }); }) .catch((err) => console.log(err)); } @@ -95,13 +96,11 @@ router.get("/categories", ensureAuthenticated, function (req, res) { } console.log(donor); const allCategories = item.getAttributes().category?.defaultValue; - res.render("stock", { - data: { - name: req.user.name, - categories: allCategories, - donorId: donor, - donorName: donor_name, - }, + res.json({ + name: req.user.name, + categories: allCategories, + donorId: donor, + donorName: donor_name, }); } }); @@ -230,9 +229,11 @@ router.post("/checkout", ensureAuthenticated, function (req, res) { itemName + " has expired. Select other item.", }); - res.render("checkoutdetails", { - data: { name: req.user.name, items: allItems, errors }, - }); + res.json({ + name: req.user.name, + items: allItems, + errors: errors + }); } }); } else if (chicoId.length != 9) { @@ -244,9 +245,11 @@ router.post("/checkout", ensureAuthenticated, function (req, res) { if (err) { console.log("THIS IS ERRROR " + err); } else { - res.render("checkoutdetails", { - data: { name: req.user.name, items: allItems, errors }, - }); + res.json({ + name: req.user.name, + items: allItems, + errors: errors + }); } }); } else { @@ -266,8 +269,9 @@ router.post("/checkout", ensureAuthenticated, function (req, res) { if (err) { console.log("THIS IS ERRROR " + err); } else { - res.render("checkoutdetails", { - data: { name: req.user.name, items: allItems, errors }, + res.json({ + name: req.user.name, + items: allItems }); } }); @@ -307,9 +311,7 @@ router.post("/checkout", ensureAuthenticated, function (req, res) { console.log("THIS IS ERRROR " + err); } else { console.log(" ALL ITEMS => " + allItems); - res.render("checkout_success", { - data: { name: req.user.name, items: allItems, errors }, - }); + res.status(200).json({ items: allItems }); } }); //res.render('checkout_success', {data: {}}); @@ -336,9 +338,10 @@ router.get("/checkout", function (req, res) { // console.log(" forward to checkout"); // console.log(allItems); - res.render("checkoutdetails", { - data: { name: "Subhed", items: allItems }, - }); + res.json({ + name: "Subhed", + items: allItems + }); } console.log("OUTSIDE"); @@ -378,16 +381,14 @@ router.get("/charts", ensureAuthenticated, function (req, res) { } else { // console.log(allItems1); - res.render("charts", { - data: { - name: req.user.name, - allItems, - cat: result, - stock: allItems0, - items: allItems1, - expired: expItems, - }, - }); + res.json({ + name: req.user.name, + allItems: allItems, + cat: result, + stock: allItems0, + items: allItems1, + expired: expItems + }); return; } From a31157e88156840552b58369077d67a54f931511 Mon Sep 17 00:00:00 2001 From: shalavritha mamunooru Date: Wed, 26 Apr 2023 11:35:36 -0700 Subject: [PATCH 5/6] Updated users.js for new user sign up and user already exists, Fixes #130 --- backend/src/routes/users.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/users.js b/backend/src/routes/users.js index b9e3448b..393bd73d 100644 --- a/backend/src/routes/users.js +++ b/backend/src/routes/users.js @@ -37,9 +37,9 @@ router.post('/sign_up', function (req, res) { } }).then(([user, created]) => { if (!created) { - let errors = []; - errors.push('User Exist!' ); - res.render('signup', { errors }); + // let errors = []; + // errors.push('User Exist!' ); + res.status(409).json({ error: 'User Exist!' }); } else { bcrypt.genSalt(10, (err, salt) => @@ -52,7 +52,8 @@ router.post('/sign_up', function (req, res) { console.log(user); - return res.render('./signup_success', { title: 'Express' }); + return res.status(201).json({ user }); + } }); From d4bb17c509d8c069cb05af94eebe3801363be43b Mon Sep 17 00:00:00 2001 From: shalavritha mamunooru Date: Thu, 27 Apr 2023 11:24:30 -0700 Subject: [PATCH 6/6] Updated users.js file to appropriate json response, Fixes #130 --- backend/src/routes/users.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/backend/src/routes/users.js b/backend/src/routes/users.js index 393bd73d..cf88afb9 100644 --- a/backend/src/routes/users.js +++ b/backend/src/routes/users.js @@ -15,12 +15,11 @@ initModels(sequelize); router.get('/register', function (req, res, next) { if (!req.isAuthenticated()) { - let errors = []; - res.render('signup', { errors }); - } - else { - res.redirect('/home'); - } + let errors = ['User not authenticated']; + res.status(400).json({ errors }); + } else { + res.status(200).json({ message: 'Redirect to /home' }); + } }); router.post('/sign_up', function (req, res) { @@ -61,7 +60,7 @@ router.post('/sign_up', function (req, res) { router.get('/login', function (req, res, next) { - res.render('signup_success', { title: 'Express' }); + res.status(200).json({ message: 'Signup successful'}); }); router.post('/login', function (req, res, next) { @@ -81,8 +80,7 @@ router.post('/login', function (req, res, next) { router.get('/logout', function (req, res) { - req.logout(); - res.render('index', { title: 'Express' }); + res.status(200).send({ title: 'Express' }); });