Skip to content
Snippets Groups Projects
Commit b91fd978 authored by Jesus Galaz's avatar Jesus Galaz
Browse files

Adding logout functionability

parent afd9f0bb
No related branches found
No related tags found
1 merge request!5User authentication implemented
// 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
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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment