简体   繁体   English

req.flash() 在 req.session.destroy() 之后不工作

[英]req.flash() not working after req.session.destroy()

Based on a condition, I need to destroy user's current session, and redirect him to a login page with a message.根据条件,我需要销毁用户当前的 session,并将他重定向到带有消息的登录页面。 I use flash to have a show-once-only message.我使用flash来获得只显示一次的消息。 Everywhere, this works fine on my app except here, because here I use req.flash after req.session.destroy() .除了这里,这在我的应用程序上无处不在,因为在这里我在req.session.destroy()之后使用req.flash

How can I achieve this?我怎样才能做到这一点? I have tried placing req.flash() before req.session.destroy() but that does not work, I think because req.session.destroy() clears the session where we just stored the flash message.我曾尝试将req.flash()放在 req.session.destroy() 之前, req.session.destroy()不起作用,我认为是因为req.session.destroy()清除了我们刚刚存储 flash 消息的 session。 Thanks.谢谢。

   if( req.session.adminUser.blocked ){
        req.logout();
        req.session.destroy(()=>{
            req.flash("flashMessage", "You are blocked, Please contact admin");
            req.session.save(function(){
                res.redirect("/admin-panel/login");
                return false;
            });//session.save()
        });
    }

With this code, I get this error - Error: req.flash() requires sessions .使用此代码,我收到此错误 - Error: req.flash() requires sessions If I move the flash statement before the session.destroy statement, then I do not get any error, but the message is not shown.如果我将 flash 语句移动到session.destroy语句之前,那么我不会收到任何错误,但不会显示消息。

If you look at flash source you can see that this middleware REQUIRE session to work如果您查看flash 源代码,您可以看到此中间件需要session才能工作

req.flash() depends on a valid session, so if you destroy that session, the flash messages are destroyed as well. req.flash()取决于有效的 session,因此如果您销毁该 session,则 flash 消息也会被销毁。

It seems to me that it might actually be better to create a separate "blocked" page, and render that, instead of redirecting the blocked used back to the login page (where they can't log in anyway):在我看来,创建一个单独的“被阻止”页面并呈现它实际上可能更好,而不是将被阻止的用户重定向回登录页面(无论如何他们都无法登录):

req.session.save(function(){
  res.render("/admin-panel/blocked");
});

As a (less-pretty) alternative, you can pass a query string to the login page indicating that the user was blocked:作为(不太漂亮的)替代方案,您可以将查询字符串传递到登录页面,指示用户被阻止:

res.redirect("/admin-panel/login?blocked=true");

And use a check in the login-template to see if it should show a "you are blocked" message.并检查登录模板,看看它是否应该显示“您被阻止”的消息。

I realize this is an old post by now, but in case it helps someone.我现在意识到这是一篇旧文章,但以防万一它对某人有帮助。

It looks like using req.logout();看起来像使用req.logout(); does the trick, this allows req.flash();做的伎俩,这允许req.flash(); to be called afterwards effectively.之后有效地调用。

"Node version": "14.16.1"
"express": "4.17.2"
"express-session": "1.17.2"
"passport": "0.5.2"
"connect-flash": "0.1.1"
"cookie-parser": "1.4.6"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM