diff --git a/src/index.js b/src/index.js index 8b1ce005412fc080f7cb1391bced63fe28f1ff0e..d6bbf88d263dcafef69454e82fcfe06f8f557453 100644 --- a/src/index.js +++ b/src/index.js @@ -66,8 +66,7 @@ if(existingUser){ } }) - -//Login User +//login app.post("/login", async (req, res) => { try { const { username, password } = req.body; @@ -76,10 +75,6 @@ app.post("/login", async (req, res) => { if (!user) { return res.send("Usuario no encontrado"); } - // Asegúrate de que tasks sea un array vacÃo si no existe - if (!user.tasks) { - user.tasks = []; - } const isPasswordMatch = await bcrypt.compare(password, user.password); if (!isPasswordMatch) { @@ -87,12 +82,15 @@ app.post("/login", async (req, res) => { } // Guardar el ID del usuario en la sesión - req.session.userId = user._id; + req.session.userId = user._id; console.log("Sesión después de asignar userId:", req.session); - // Redirigir al To-Do List (home.ejs) - res.render("home", { tasks: user.tasks }); + // 🔹 Obtener las tareas del usuario desde la colección "tasks" + const tasks = await Task.find({ userId: user._id }); + + // Redirigir al To-Do List con las tareas cargadas + res.render("home", { tasks }); } catch (error) { console.error("Error en login:", error); @@ -103,6 +101,7 @@ app.post("/login", async (req, res) => { + // PARTE TODO LIST app.get("/home", async (req, res) => { if (!req.session.userId) { @@ -110,8 +109,11 @@ app.get("/home", async (req, res) => { } try { - // Obtener las tareas del usuario - const tasks = await Task.find({ userId: req.session.userId }); + // Convertir userId en un ObjectId válido + const userId = new mongoose.Types.ObjectId(req.session.userId); + + // Obtener las tareas del usuario desde MongoDB + const tasks = await Task.find({ userId }); // Mostrar las tareas en la vista res.render("home", { tasks });