Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • gajesh24/cloudcomputing_act1
1 result
Show changes
Commits on Source (1)
Showing
with 57 additions and 27 deletions
File moved
File moved
File moved
File moved
File moved
File moved
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const Todo = require('../models/todo'); const Todo = require("../models/todo");
// Middleware to verify if the user is authenticated // Middleware to verify if the user is authenticated
function isAuthenticated(req, res, next) { function isAuthenticated(req, res, next) {
if (req.session.user) { if (req.session.user) {
return next(); return next();
} else { } else {
return res.status(401).json({ error: 'Unauthorized' }); return res.status(401).json({ error: "Unauthorized" });
} }
} }
// Route to logout // Route to logout
router.post('/logout', (req, res) => { router.post("/logout", (req, res) => {
req.session.destroy((err) => { req.session.destroy((err) => {
if (err) { if (err) {
console.error('Error al destruir la sesión:', err); console.error("Error al destruir la sesión:", err);
return res.status(500).json({ error: 'Error al cerrar sesión' }); return res.status(500).json({ error: "Error al cerrar sesión" });
} }
res.status(200).json({ message: 'Logout successful' }); res.status(200).json({ message: "Logout successful" });
}); });
}); });
// Route to obtain all tasks of authenticated user // Route to obtain all tasks of authenticated user
router.get('/', isAuthenticated, async (req, res) => { router.get("/", isAuthenticated, async (req, res) => {
try { try {
const todos = await Todo.find({ userId: req.session.user.id }); const todos = await Todo.find({ userId: req.session.user.id });
res.json(todos); res.json(todos);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ error: 'Server error' }); res.status(500).json({ error: "Server error" });
} }
}); });
// Route to add a task // Route to add a task
router.post('/', isAuthenticated, async (req, res) => { router.post("/", isAuthenticated, async (req, res) => {
const { description } = req.body; const { description } = req.body;
try { try {
const todo = new Todo({ description, userId: req.session.user.id }); const todo = new Todo({ description, userId: req.session.user.id });
await todo.save(); await todo.save();
res.json(todo); res.json(todo);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ error: 'Server error' }); res.status(500).json({ error: "Server error" });
} }
}); });
// Route to eliminate tasks // Route to eliminate tasks
router.delete('/:id', isAuthenticated, async (req, res) => { router.delete("/:id", isAuthenticated, async (req, res) => {
try { try {
const todo = await Todo.findById(req.params.id); const todo = await Todo.findById(req.params.id);
if (!todo || todo.userId.toString() !== req.session.user.id) { if (!todo || todo.userId.toString() !== req.session.user.id) {
return res.status(404).json({ error: 'Todo not found or unauthorized' }); return res.status(404).json({ error: "Todo not found or unauthorized" });
} }
await todo.deleteOne(); await todo.deleteOne();
res.json({ success: true }); res.json({ success: true });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ error: 'Server error' }); res.status(500).json({ error: "Server error" });
} }
}); });
// Route to update the description of a task // Route to update the description of a task
router.put('/:id/description', isAuthenticated, async (req, res) => { router.put("/:id/description", isAuthenticated, async (req, res) => {
const { description } = req.body; const { description } = req.body;
try { try {
const todo = await Todo.findById(req.params.id); const todo = await Todo.findById(req.params.id);
if (!todo || todo.userId.toString() !== req.session.user.id) { if (!todo || todo.userId.toString() !== req.session.user.id) {
return res.status(404).json({ error: 'Todo not found or unauthorized' }); return res.status(404).json({ error: "Todo not found or unauthorized" });
} }
todo.description = description; todo.description = description;
...@@ -82,19 +81,19 @@ router.put('/:id/description', isAuthenticated, async (req, res) => { ...@@ -82,19 +81,19 @@ router.put('/:id/description', isAuthenticated, async (req, res) => {
res.json({ todo }); res.json({ todo });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ error: 'Server error' }); res.status(500).json({ error: "Server error" });
} }
}); });
// Route to mark a task as completed // Route to mark a task as completed
router.put('/:id/done', isAuthenticated, async (req, res) => { router.put("/:id/done", isAuthenticated, async (req, res) => {
const { isDone } = req.body; const { isDone } = req.body;
try { try {
const todo = await Todo.findById(req.params.id); const todo = await Todo.findById(req.params.id);
if (!todo || todo.userId.toString() !== req.session.user.id) { if (!todo || todo.userId.toString() !== req.session.user.id) {
return res.status(404).json({ error: 'Todo not found or unauthorized' }); return res.status(404).json({ error: "Todo not found or unauthorized" });
} }
todo.isDone = isDone; todo.isDone = isDone;
...@@ -103,8 +102,8 @@ router.put('/:id/done', isAuthenticated, async (req, res) => { ...@@ -103,8 +102,8 @@ router.put('/:id/done', isAuthenticated, async (req, res) => {
res.json({ todo }); res.json({ todo });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
res.status(500).json({ error: 'Server error' }); res.status(500).json({ error: "Server error" });
} }
}); });
module.exports = router; module.exports = router;
\ No newline at end of file
File moved
const express = require('express'); const express = require("express");
const path = require('path'); const path = require("path");
const connectDB = require('./mongodb'); const connectDB = require("./mongodb");
const session = require('express-session'); const session = require("express-session");
const app = express(); const app = express();
// Middleware // Middleware
app.use(express.json()); app.use(express.json());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, "public")));
// Session configuration // Session configuration
app.use( app.use(
session({ session({
secret: 'session_secret', secret: "session_secret",
resave: false, resave: false,
saveUninitialized: false, saveUninitialized: false,
cookie: { secure: false } cookie: { secure: false },
}) })
); );
...@@ -23,8 +23,8 @@ app.use( ...@@ -23,8 +23,8 @@ app.use(
connectDB(); connectDB();
// Routes // Routes
app.use('/api/todos', require('./routes/todos')); app.use("/api/todos", require("./routes/todos"));
app.use('/api/users', require('./routes/users')); app.use("/api/users", require("./routes/users"));
// Initialize the server // Initialize the server
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
......
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved