Skip to content
3 changes: 2 additions & 1 deletion backend/src/routes/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions backend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down Expand Up @@ -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 });

}
}
}
Expand Down
73 changes: 37 additions & 36 deletions backend/src/routes/sale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
});
Expand Down Expand Up @@ -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));
}
Expand All @@ -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,
});
}
});
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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
});
}
});
Expand Down Expand Up @@ -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: {}});
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 12 additions & 13 deletions backend/src/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) =>
Expand All @@ -52,15 +51,16 @@ router.post('/sign_up', function (req, res) {


console.log(user);
return res.render('./signup_success', { title: 'Express' });
return res.status(201).json({ user });

}

});
});


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) {
Expand All @@ -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' });
});


Expand Down