Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CloudComputing_Act1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jesus Galaz Reyes
CloudComputing_Act1
Commits
b91fd978
Commit
b91fd978
authored
7 months ago
by
Jesus Galaz
Browse files
Options
Downloads
Patches
Plain Diff
Adding logout functionability
parent
afd9f0bb
No related branches found
No related tags found
1 merge request
!5
User authentication implemented
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
public/js/app.js
+17
-0
17 additions, 0 deletions
public/js/app.js
public/js/index.js
+17
-0
17 additions, 0 deletions
public/js/index.js
routes/todos.js
+18
-25
18 additions, 25 deletions
routes/todos.js
with
52 additions
and
25 deletions
public/js/app.js
+
17
−
0
View file @
b91fd978
// 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
This diff is collapsed.
Click to expand it.
public/js/index.js
0 → 100644
+
17
−
0
View file @
b91fd978
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
This diff is collapsed.
Click to expand it.
routes/todos.js
+
18
−
25
View file @
b91fd978
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment