diff --git a/frontend/js/chat.js b/frontend/js/chat.js index 52a146c..7acf787 100644 --- a/frontend/js/chat.js +++ b/frontend/js/chat.js @@ -12,11 +12,19 @@ window.onload = function() { } logoutButton.onclick = logout; - function logout() { - cookieStore.delete('token'); - cookieStore.delete('io'); - location.reload(); + fetch('/auth/logout', { + method: 'POST', + credentials: 'include' + }) + .then(response => { + if (response.ok) { + window.location.href = '/login'; + } + }) + .catch(error => { + console.error('Logout failed:', error); + }); } async function getToken() { diff --git a/index.js b/index.js index b9e5355..54e3801 100644 --- a/index.js +++ b/index.js @@ -45,14 +45,17 @@ app.post('/auth/signup', async (req, res) => { }); // logout API -app.post('/auth/logout', (req, res) => { - res.clearCookie('token'); - req.session.destroy((err) =>{ - if (err) { - console.log(err); - } - }) -}) + app.post('/auth/logout', (req, res) => { + // clear JWT token + res.clearCookie('token', { + path: '/' + }); + // clear socket.io cookie (no idea what is it for) + res.clearCookie('io', { + path: '/' + }); + res.status(200).json({ message: 'Successfully logged out'}); + }) // get JWT token API app.get('/auth/token', (req, res) => {