diff --git a/public/js/app.js b/public/js/app.js index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e76b1042dcc3e03f55e57e2bf9d7d4f958a95566 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 0000000000000000000000000000000000000000..ad7a7337b904f4a0168684e808990d5f219aec34 --- /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 49a3ea1b9d1095ee27014f0bf20d6505b62fbc13..fb0286b6359e2e24a78e4e1be0a18fed2fa15857 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