Answer the question
In order to leave comments, you need to log in
How to delete a session when the site closes?
When entering the site, the user checks the box to delete the session when the site is closed or not.
If I delete it, then the following code works for me on the client
window.addEventListener("unload", () => {
let xhr = new XMLHttpRequest();
xhr.open('GET', '/user/sessionDestroy');
xhr.send();
});
router.get('/sessionDestroy', async (req, res) => {
try {
if (req.session && req.session.user) {
const candidate = await User.findOne({email: req.session.user.email})
if (candidate.remainInSystem) {
// console.log('remainInSystem');
return
}
}
console.log('deleting session');
req.session.destroy();
res.status(200).send('ok');
} catch (error) {
console.log(error);
}
})
window.addEventListener("unload", () => {
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question