克服React中的登录中间件



i是从local主机登录的使用对另一个localhost使用express的

嗨,我正在使用此代码通过React发布登录数据:

 handleSubmit(event) {
    event.preventDefault();
  var params = Object.entries(this.state).map(([key, val]) => `${key}=${val}`).join('&')
  fetch("http://localhost:3006/login", {
    method: 'POST',
    headers: {'Content-Type':'application/x-www-form-urlencoded'},
    body: params
  })
  .then((response) => response.json())
  .then((responseJson) => {
    //this.props.history.push("/landing");
    console.log("loggedin", responseJson);
  })
  }

和其他本地主机后端是:

router.post("/login", passport.authenticate("local",{successRedirect: "/landing",failureRedirect: "login"}) ,function(req, res){
console.log(req.body);
});

发送后,我需要方法来保存登录数据,以便可以登录到页面/着陆。正常地删除中间件时,代码工作。

router.get("/landing",middleware.isLoggedIn, function(req, res) {

因此,有任何反应专家可以帮助我做到:)

保存用户数据(用户会话)的一种方法是" WebTokens"。你可以使用jsonwebtoken让服务器识别用户并知道有关他的一些数据。这些数据被哈希并存储在浏览器中。从浏览器(客户端)到服务器的任何查询都将携带此数据,服务器将读取并使用任何有用的数据。

有关如何与Express Server一起使用的更多信息,请更深入地了解此示例。

最新更新