From 6de41474bda67bd22a9d687a302f760b59cde2be Mon Sep 17 00:00:00 2001 From: Andrew Butler Date: Thu, 26 Oct 2023 20:45:59 -0600 Subject: [PATCH] trying to fix an error --- app.js | 8 ++++++++ routes/affirmationRoutes.js | 17 ++++------------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app.js b/app.js index 27026f2..92d3330 100644 --- a/app.js +++ b/app.js @@ -62,6 +62,14 @@ app.get("/test-flash", (req, res) => { res.redirect("/login"); }); +function ensureAuthenticated(req, res, next) { + if (req.isAuthenticated()) { + return next(); + } + req.flash("error_msg", "Please log in to view this resource"); + res.redirect("/login"); +} + // Error handling app.use((err, req, res, next) => { console.error(`[Error] ${err.stack}`); diff --git a/routes/affirmationRoutes.js b/routes/affirmationRoutes.js index 0850f88..224c222 100644 --- a/routes/affirmationRoutes.js +++ b/routes/affirmationRoutes.js @@ -11,18 +11,12 @@ router.get("/affirmation-setup", async (req, res) => { if (!user || !user.affirmations) { const userId = req.session.passport.user; - user = await User.findById(userId); - user = user ? JSON.parse(JSON.stringify(user)) : null; // Deep clone to plain object + user = await User.findById(userId).select( + "username affirmationDuration affirmations" + ); // Explicitly select fields + user = user ? user.toObject() : null; // Convert to plain object } - // Debugging Step 1: Check the Conversion - console.log( - "Is Plain Object?", - user instanceof Object && - !(user instanceof Array) && - !(user instanceof Function) - ); - const categoriesWithSpaces = {}; if (user && user.affirmations) { for (const [key, value] of Object.entries(user.affirmations)) { @@ -30,9 +24,6 @@ router.get("/affirmation-setup", async (req, res) => { } } - // Debugging Step 2: Check the Data - console.log("Categories with Spaces:", categoriesWithSpaces); - res.render("affirmation-setup", { user, categoriesWithSpaces }); });