请求会话销毁()在Nodejs中不起作用

7lrncoxx  于 2023-02-03  发布在  Node.js
关注(0)|答案(7)|浏览(167)

尝试通过在Nodejs中使用**req.session.destroy()**销毁会话来执行注销操作,但不起作用。

节点代码:

router.get('/logout', function(req, res) {
  req.session.destroy();
  console.log(session.email);
  res.redirect('/signup');
});

我正在从我的html文件调用/logout API。

HTML代码:-

<div class="container-fluid">
        <header class="row top text-center">
            <div class="col-md-2 righttopnav" >
                <a href="index" style="font-size:25px;">Auto<b>SecT</b></a>
            </div>
            <div class="col-md-10 topnav" id="myTopnav">
                <%if(session.email){%>
                <a href="/logout">Logout</a>
                <% } else { %>
                <a href="/signup">Signup / Login</a>
                <% } %>
                <a href="#career">Career</a>
                <a href="#clients">Clients</a>
                <a href="#services">Services</a>
            </div>
        </header>

        <section class="content">   
            <div class="row text-center">
                <div class="col-md-12 " >
                    <p class="p1"><%-session.email%>Scan your Website or APP with just a <b>click!</b></p>
                </div>
            </div>
        </section>

        <div class="row text-center" style="padding-top:5%;">
            <div class="col-md-4">
                <i class="fa fa-globe fa-5x" aria-hidden="true" style="color:#c2c2a3"></i>
                <br>
                <input class="url" type="url"  placeholder="Enter your url" style="border-radius: 20px ">
            </div>  
            <div class="col-md-4">
                <p class="p">OR</p>
            </div>
            <div class="col-md-4">
                <i class="fa fa-file-o fa-5x" aria-hidden="true" style="color:#c2c2a3; padding-bottom: 4% "></i>
                <br>
                <label class="btn btn-primary btn-sm  btn-file upload">
                <i class="fa fa-upload fa-2x" aria-hidden="true"></i>
                <input type="file" style="display: none;">
                </label>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="col-md-12 text-center" style="padding-top: 5%; margin-bottom: 1%; border-radius: 20%;">
                <input type="submit" id="button" class="btn btn-success btn-md" value="Scan">
            </div>
            </div>
        </div>
</div>

/logout将我重定向到所需页面,但会话不会被销毁。

k97glaaz

k97glaaz1#

请在会话销毁后重定向。您必须在回调中移动重定向代码。

router.get('/logout', function(req, res) {
  req.session.destroy(function(err){
     if(err){
        console.log(err);
     }else{
         console.log(session.email);
         req.end();
         res.redirect('/signup');
     }
  });

});
mdfafbf1

mdfafbf12#

这对我很有效。

.get('/logout', function (req, res) {
    req.logout();
    req.session = null; 
    res.redirect('/back/to/login');
});
xn1cxnb4

xn1cxnb43#

尝试销毁会话并注销:

req.session.destroy();
 req.logout();
 res.redirect('/signup');
yh2wf1be

yh2wf1be4#

此解决方案对我很有效:

req.session.destroy(function () {
    req.logout();
    res.redirect('/');              
});
guykilcj

guykilcj5#

我做了两件事,并得到了它的工作。首先,我设置unset为destroy在会话选项。其次,删除会话如下

router.get('/logout', function(req, res) {
        delete req.session;
        res.redirect('/signup');
    });

参考https://github.com/expressjs/session

bogh5gae

bogh5gae6#

在注销时不应使用href<a href="/logout">Logout</a>
只需创建一个<p><button>
您的请求中存在冲突。
然后,您的res.redirect('/signup');应该可以工作。

kg7wmglp

kg7wmglp7#

尝试使用这个,早期的req.session.destroy工作正常,但更新后,它不与passport和express-session.有req.logout嵌套req.session.destroy工作正如其他人指出的.

const logout = async (req, res) => {
  req.session.destroy(function (err) {
    if (err) {
      console.log(`unable to destroy session`, err);
    }
    if (!err) {
      req.logout((error) => {
        if (error) console.log(`unable to logout`, error);
      });
      res.redirect("/login");
    }
  });
};

相关问题