注销用户出现问题。带有JWT auth的Express服务器api

注销用户出现问题。带有JWT auth的Express服务器api,express,logout,Express,Logout,我正在尝试创建一个完整的mern应用程序堆栈。我使用jwt身份验证。我将把令牌保存在本地存储器中,然后使用react将令牌从本地存储器中删除 我试过req.logout()、req.logout()和req.session.destroy(),但都不起作用。 谢谢大家! const signIn = (req, res) => { const { email, password } = req.body; User.findOne({ email: email }) .the

我正在尝试创建一个完整的mern应用程序堆栈。我使用jwt身份验证。我将把令牌保存在本地存储器中,然后使用react将令牌从本地存储器中删除

我试过req.logout()、req.logout()和req.session.destroy(),但都不起作用。 谢谢大家!

const signIn = (req, res) => {
  const { email, password } = req.body;

  User.findOne({ email: email })
  .then((user) => {
    if (!user) {
      const error = new Error('A user with this email could not be found');
      error.statusCode = 401;
      throw error;
    }

    if(!user.authenticate(password)) {
      const error = new Error('A user with this email could not be found');
      error.statusCode = 401;
      throw error;
    }

    const token = jwt.sign({ 
        email: user.email,
        userId: user._id.toString()
      }
      , 'somesupersecret'
      , { expiresIn: '1h' });

      res.status(200).json(
        { 
          message: 'User successfully logged in!', 
          token,
          userId: user._id.toString()
        });
      })
      .catch(error => {
        if (!error.statusCode) {
          error.statusCode = 500;
          next(error);
      }

    })
}

const logOut = async(req, res) => {
  try{
    await req.logOut();
    res.status(200).json(
    { 
      message: 'User successfully logged out!',        
    });
  }
  catch(error) {
    console.log(error);
  }
}

module.exports = {
  signUp,
  signIn,
  logOut
}

我变成了错误,比如TypeError:req.logOut不是一个函数。

这不应该在服务器上完成。只需删除/使客户端发送的jwt令牌无效。通常,令牌存储在本地存储和/或cookie中。只需删除该令牌并刷新页面,用户将注销。

这不应在服务器上完成。只需删除/使客户端发送的jwt令牌无效。通常,令牌存储在本地存储和/或cookie中。只要删除该令牌并刷新页面,用户就会注销。

谢谢,但它可靠吗?当然可以。根据我的经验,这是非常安全的。好的,但是刷新单页应用程序可以吗?在注销的情况下,可以。如果你仔细观察,你会发现facebook也是如此。这取决于你的情况。您只需删除令牌,然后使用react router以编程方式导航到另一个页面,并根据自己的喜好处理状态/道具。谢谢,但它可靠吗?当然可以。根据我的经验,这是非常安全的。好的,但是刷新单页应用程序可以吗?在注销的情况下,可以。如果你仔细观察,你会发现facebook也是如此。这取决于你的情况。您只需删除令牌,然后使用react router以编程方式导航到另一个页面,并根据需要处理状态/道具。