Node js session destroy does not redirect

I am writting login system but when i click logout button website is opening same page.

<form action="out" method="POST">
    <button type="submit" class="dropdown-item py-3 pr-5">logout</button>
</form>

server.js:

app.post('/out', function(request, response) {
    request.session.destroy();
    response.redirect('/');
});
app.get('/', function(request, response) {
    if (request.session.loggedin) {
        response.sendFile(path.join(__dirname + '/public/chat.html'));
    } else {
        response.sendFile(path.join(__dirname + '/public/index.html'));
    }
});

Website every time opening chat.html but If I full refresh the site, web site opening index.html.

Upvotes: 1

Views: 474

Answers (1)

sayalok
sayalok

Reputation: 920

app.post('/out', function(request, response) {
    request.session.destroy(err => {
        if(err) {
             console.log(err)
         }else{
             response.redirect('/signup');
         }
    });
});

U can try this. First destroy the session then call a callback function inside there check if there is any issue in destroying session if not then redirect

Upvotes: 2

Related Questions