Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs PassportJS Express req.user未在Heroku中持久化_Reactjs_Express_Heroku_Passport.js_Backend - Fatal编程技术网

Reactjs PassportJS Express req.user未在Heroku中持久化

Reactjs PassportJS Express req.user未在Heroku中持久化,reactjs,express,heroku,passport.js,backend,Reactjs,Express,Heroku,Passport.js,Backend,我正在使用React和Express。只有React托管在heroku上,而我在本地托管Express服务器。对于Express,我使用的是MongoDB的Passportjs。 问题是React客户端在本地部署上运行良好,但是一旦我在Heroku上部署React应用程序,它就无法正常运行使用部署的React应用程序时,我可以注册用户,但无法登录用户。它不会返回错误,POST(“/login”)会将“已成功验证”返回给我,当我尝试使用axios访问req.user时,它不会在应该返回用户时返回任

我正在使用React和Express。只有React托管在heroku上,而我在本地托管Express服务器。对于Express,我使用的是MongoDB的Passportjs。

问题是React客户端在本地部署上运行良好,但是一旦我在Heroku上部署React应用程序,它就无法正常运行

使用部署的React应用程序时,我可以注册用户,但无法登录用户。它不会返回错误,POST(“/login”)会将“已成功验证”返回给我,当我尝试使用axios访问req.user时,它不会在应该返回用户时返回任何内容

反应代码

const getUser = () => {
    axios({
      method: "GET",
      withCredentials: true,
      url: "http://localhost:3001/user",
    })
      .then((res) => {
        {
          console.log(res.data);
          if (res.data.username != null)
            setMessage("Welcome " + res.data.username);
        }
      })
      .catch((err) => console.log(err));
  };

  const loginTheUser = async () => {
    await axios({
      method: "POST",
      data: {
        username: username,
        password: password,
      },
      withCredentials: true,
      url: "http://localhost:3001/login",
    }).then((res) => {
      if (res.data === "Successfully Authenticated") {
   
        history.push("/");
      } 

      console.log(res.data);
    });

    await getUser();
  };
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
  cors({
    origin: [
      "http://localhost:3000",
      "https://my-herokuapp.com",
    ], // <-- location of the react app were connecting to
    credentials: true,
  })
);


app.use(
  session({
    secret: "secretcode",
    resave: true,
    saveUninitialized: true,
  })
);
app.use(cookieParser("secretcode"));
app.use(passport.initialize());
app.use(passport.session());
require("./passportConfig")(passport);
快速代码

const getUser = () => {
    axios({
      method: "GET",
      withCredentials: true,
      url: "http://localhost:3001/user",
    })
      .then((res) => {
        {
          console.log(res.data);
          if (res.data.username != null)
            setMessage("Welcome " + res.data.username);
        }
      })
      .catch((err) => console.log(err));
  };

  const loginTheUser = async () => {
    await axios({
      method: "POST",
      data: {
        username: username,
        password: password,
      },
      withCredentials: true,
      url: "http://localhost:3001/login",
    }).then((res) => {
      if (res.data === "Successfully Authenticated") {
   
        history.push("/");
      } 

      console.log(res.data);
    });

    await getUser();
  };
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
  cors({
    origin: [
      "http://localhost:3000",
      "https://my-herokuapp.com",
    ], // <-- location of the react app were connecting to
    credentials: true,
  })
);


app.use(
  session({
    secret: "secretcode",
    resave: true,
    saveUninitialized: true,
  })
);
app.use(cookieParser("secretcode"));
app.use(passport.initialize());
app.use(passport.session());
require("./passportConfig")(passport);
护照配置

const User = require("./user");
const bcrypt = require("bcryptjs");
const localStrategy = require("passport-local").Strategy;

module.exports = function (passport) {
  passport.use(
    new localStrategy((username, password, done) => {
      User.findOne({ username: username }, (err, user) => {
        if (err) throw err;
        if (!user) return done(null, false);
        bcrypt.compare(password, user.password, (err, result) => {
          if (err) throw err;
          if (result === true) {
            return done(null, user);
          } else {
            return done(null, false);
          }
        });
      });
    })
  );

  passport.serializeUser((user, cb) => {
    cb(null, user.id);
  });
  passport.deserializeUser((id, cb) => {
    User.findOne({ _id: id }, (err, user) => {
      const userInformation = {
        username: user.username,
      };
      cb(err, userInformation);
    });
  });
};
const User = require("./user");
const bcrypt = require("bcryptjs");
const localStrategy = require("passport-local").Strategy;

module.exports = function (passport) {
  passport.use(
    new localStrategy((username, password, done) => {
      User.findOne({ username: username }, (err, user) => {
        if (err) throw err;
        if (!user) return done(null, false);
        bcrypt.compare(password, user.password, (err, result) => {
          if (err) throw err;
          if (result === true) {
            return done(null, user);
          } else {
            return done(null, false);
          }
        });
      });
    })
  );

  passport.serializeUser((user, cb) => {
    cb(null, user.id);
  });
  passport.deserializeUser((id, cb) => {
    User.findOne({ _id: id }, (err, user) => {
      const userInformation = {
        username: user.username,
      };
      cb(err, userInformation);
    });
  });
};