From b91fd978e58227b53f5ed4dd056194f39b222bb2 Mon Sep 17 00:00:00 2001 From: Jesus Galaz <jesusgalazr@icloud.com> Date: Mon, 14 Oct 2024 11:28:34 +0200 Subject: [PATCH] Adding logout functionability --- public/js/app.js | 17 +++++++++++++++++ public/js/index.js | 17 +++++++++++++++++ routes/todos.js | 43 ++++++++++++++++++------------------------- 3 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 public/js/index.js diff --git a/public/js/app.js b/public/js/app.js index e69de29..e76b104 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -0,0 +1,17 @@ +// Obtener referencia a los elementos del DOM +const taskList = document.querySelector('.task-list ul'); +const newTaskForm = document.querySelector('form'); + +// Logout +document.getElementById('logout-btn').addEventListener('click', async () => { + try { + const res = await fetch('/api/users/logout', { method: 'POST' }); + if (res.ok) { + window.location.href = 'login.html'; + } else { + console.error('Logout failed'); + } + } catch (err) { + console.error('Error during logout:', err); + } +}); \ No newline at end of file diff --git a/public/js/index.js b/public/js/index.js new file mode 100644 index 0000000..ad7a733 --- /dev/null +++ b/public/js/index.js @@ -0,0 +1,17 @@ +document.addEventListener('DOMContentLoaded', async () => { + const logoutBtn = document.getElementById('logout-btn'); + + // Evento para el botón de logout + logoutBtn.addEventListener('click', async () => { + try { + const res = await fetch('/api/users/logout', { method: 'POST' }); + if (res.ok) { + window.location.href = 'login.html'; + } else { + console.error('Logout failed'); + } + } catch (err) { + console.error('Error during logout:', err); + } + }); +}); \ No newline at end of file diff --git a/routes/todos.js b/routes/todos.js index 49a3ea1..fb0286b 100644 --- a/routes/todos.js +++ b/routes/todos.js @@ -2,32 +2,25 @@ const express = require('express'); const router = express.Router(); const Todo = require('../models/todo'); -// Get all TODOs for the logged-in user -router.get('/', async (req, res) => { - const todos = await Todo.find({ userId: req.user.id }); - res.json(todos); -}); - -// Add a new TODO -router.post('/', async (req, res) => { - const { description } = req.body; - const todo = new Todo({ description, userId: req.user.id }); - await todo.save(); - res.json(todo); -}); +// Middleware para verificar si el usuario está autenticado +function isAuthenticated(req, res, next) { + if (req.session.user) { + return next(); + } else { + return res.status(401).json({ error: 'Unauthorized' }); + } +} -// Mark TODO as done -router.put('/:id', async (req, res) => { - const todo = await Todo.findById(req.params.id); - todo.isDone = true; - await todo.save(); - res.json(todo); -}); -// Delete a TODO -router.delete('/:id', async (req, res) => { - await Todo.findByIdAndDelete(req.params.id); - res.json({ success: true }); +router.post('/logout', (req, res) => { + req.session.destroy((err) => { + if (err) { + console.error('Error al destruir la sesión:', err); + return res.status(500).json({ error: 'Error al cerrar sesión' }); + } + // Redirigir al cliente o enviar una respuesta exitosa + res.status(200).json({ message: 'Logout successful' }); + }); }); -module.exports = router; +module.exports = router; \ No newline at end of file -- GitLab