0

Log out route

router.get('/logout', (req, res) => { delete req.session.returnTo; req.flash('success', 'Successfully Logged Out!'); req.session.destroy(); res.redirect('/home'); }) 

Route for rendering home view

app.get('/home',(req,res) =>{ const isLoggedIn = req.session.user ? true : false; res.render('home', { isLoggedIn }); }) 

Home view

<% } if (!isLoggedIn) { %> <% if (success.length > 0 ) {%> <div class="alert alert-success alert-dismissible fade show" role="alert"> <%= success %> <alert type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></alert> </div> <% } %> <% } %> 

Explaination of main issue

My flahes are working fine for every other route and view for example login and signup the only issue i am facing is that in my logout route after deleting the session the flash message does not appear

when i removed the code req.session.destroy(); the flash message worked fine i a;so searched internet to find solution but didnt find something usefull. your help will be appreciated a lot.

5
  • I haven't got a chance to test your code yet but one question comes on top of my head is Can we put more than 1 request(req) in a code? I mean, do you currently have working a code where there are two (or more) request? Commented Jun 15, 2024 at 0:51
  • How did you configure express-session and connect-flash? Share the code about these two configurations. Commented Jun 15, 2024 at 5:40
  • @FanoFN yes i am using two req at the same time in my code Commented Jun 15, 2024 at 9:14
  • @Subha i have configured them in my main.js file and all session requesta are working in my other routes like post ang get Commented Jun 15, 2024 at 9:15
  • 2
    The “documentation” of connect-flash says The flash is a special area of the session used for storing messages. and Flash messages are stored in the session., so it would absolutely make sense that it doesn't work anymore after destroying the session. Commented Jun 17, 2024 at 6:43

1 Answer 1

2

As we know destroying a session will affect its dependent references, therefore the flash messages if any stored in the session object will also be lost. Please see below for two workarounds:

Use a global property as below.

app.get('/logout', (req, res) => { delete req.session.returnTo; req.session.destroy(); global.location = 'logout'; res.redirect('/home'); }); app.get('/home', (req, res) => { const isLoggedIn = req.session.user ? true : false; if (global.location == 'logout') { req.flash('message', 'Successfully Logged Out!'); delete global.location; } res.render('home', { isLoggedIn }); }); 

Use redirect with a query string as shown below.

 app.get('/logout', (req, res) => { delete req.session.returnTo; req.session.destroy(); res.redirect( require('url').format({ pathname: '/home', query: { location: 'logout', }, }) ); }); app.get('/home', (req, res) => { const isLoggedIn = req.session.user ? true : false; if (req.query.location == 'logout') { req.flash('message', 'Successfully Logged Out!'); } res.render('home', { isLoggedIn }); }); 

Citations:

How do I redirect in expressjs while passing some context?

how to send flash logout message express or check if redirected

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.