Prasad Gavande
Prasad Gavande

Reputation: 259

How to redirect twice in express js?

I have added flash message on page using connect-flash.

For displaying flash message, I have redirected page to same page with following code

 req.flash('success', 'Password Changed successfully, you will be logout after 10 seconds');
 return res.redirect('/user/changepwd');

by this way, I can show flash message on the page. but after that I want user to auto logout and redirect to homepage.

How can I achieve that ? can we redirect twice with some setTimeout() ?

enter image description here

Upvotes: 0

Views: 176

Answers (1)

Prasad Gavande
Prasad Gavande

Reputation: 259

With help of Mihai's comment, redirect user from UI. i.e. ejs file as below.

<% if (sucessMessage) { %>
<script type="text/javascript">
  setTimeout(function () {
    document.location.href = "/logout";
  }, 10000);
</script>
<% } %>

with this code, if I get successMessage, then redirecting to logout page. and at express application, I have clear all cookies and sessions and then redirect it to login page again.

added one route for logout as below

router.get('/logout', userController.getLogout);

then added its controller function

  exports.getLogout = (req, res, next) => {
    if (req.session.user && req.cookies.user_sid) {
        res.clearCookie("user_sid");
        res.redirect("/login");
    }
}

Upvotes: 1

Related Questions