Skip to content
Snippets Groups Projects
Commit 74b36786 authored by Rokas Stankunas's avatar Rokas Stankunas
Browse files

Implemented basic authorization check

parent db2c99de
No related branches found
No related tags found
1 merge request!7Resolve "Enforce user authorization"
document.addEventListener('DOMContentLoaded', async () => {
// Event for checking if the user is logged in
try {
const res = await fetch('/api/users/loggedin', { method: 'POST' });
if (!res.ok) {
window.location.href = 'login.html';
}
} catch (err) {
console.error('Error while checking if a user is logged in:', err);
}
const logoutBtn = document.getElementById('logout-btn');
// Event for the logout button
......
......@@ -58,6 +58,17 @@ router.post('/login', async (req, res) => {
}
});
// Checking if the user is logged in
router.post('/loggedin', (req, res) => {
try {
if (!req.session.user) {
res.status(403).json({ error: 'Unauthorized' });
}
} catch (error) {
console.error('Authorization check failed:', error);
res.status(500).json({ error: 'Server error' });
}
});
// Closing user session
router.post('/logout', (req, res) => {
......
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