只能在对他进行身份验证后立即访问 POST 请求中的 req.user,而不能稍后在代码中访问

问题描述

当我发送发布请求并使用 passport.authenticate 时,我可以访问 req.user 但是当我稍后在代码中实际尝试 console.log() 用户时(在“/user " route) 它返回 undefined。

router.post(
  '/login',passport.authenticate('local',{
    failureRedirect: '/',}),(req,res) => {
    console.log(req.user);
    res.redirect('/');
  }
);

router.get('/user',res) => {
  console.log(req.user);
  res.send(req.user);
});

我尝试删除 expressSession 参数中的 cookie maxAge,因为有人在另一个问题上推荐了它,但它不起作用,也没有任何改变。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)