added logout API
This commit is contained in:
@@ -12,11 +12,19 @@ window.onload = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logoutButton.onclick = logout;
|
logoutButton.onclick = logout;
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
cookieStore.delete('token');
|
fetch('/auth/logout', {
|
||||||
cookieStore.delete('io');
|
method: 'POST',
|
||||||
location.reload();
|
credentials: 'include'
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (response.ok) {
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Logout failed:', error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getToken() {
|
async function getToken() {
|
||||||
|
|||||||
19
index.js
19
index.js
@@ -45,14 +45,17 @@ app.post('/auth/signup', async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// logout API
|
// logout API
|
||||||
app.post('/auth/logout', (req, res) => {
|
app.post('/auth/logout', (req, res) => {
|
||||||
res.clearCookie('token');
|
// clear JWT token
|
||||||
req.session.destroy((err) =>{
|
res.clearCookie('token', {
|
||||||
if (err) {
|
path: '/'
|
||||||
console.log(err);
|
});
|
||||||
}
|
// 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
|
// get JWT token API
|
||||||
app.get('/auth/token', (req, res) => {
|
app.get('/auth/token', (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user