diff --git a/backend/src/routes/feed.js b/backend/src/routes/feed.js index 013d17e9..90295919 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 { diff --git a/backend/src/routes/index.js b/backend/src/routes/index.js index 617f1077..5ee3c571 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,10 @@ 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 }); + } } } 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; } diff --git a/backend/src/routes/users.js b/backend/src/routes/users.js index b9e3448b..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) { @@ -37,9 +36,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 +51,8 @@ router.post('/sign_up', function (req, res) { console.log(user); - return res.render('./signup_success', { title: 'Express' }); + return res.status(201).json({ user }); + } }); @@ -60,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) { @@ -80,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' }); });