我一直在尝试使用 React 和 Node 进行登录。我使用 axios 从前端发送请求,并使用 Express 在后端发送 Passport 请求。
此代码适用于 Postman,但不适用于浏览器。req.session.passport当我从浏览器发送请求时未定义,即未调用 deserialize() 。邮递员不会发生这种情况。
axios.get('http://localhost:3001/api/admin/authenticate',{ withCredentials: true})
.then(res=>{
console.log("logged in);
this.getState(); //ignore this function just used to change state
return this.props.history.push('./Lesson_modules_Admin');
})
.catch(err=>{
console.log(err);
return this.props.history.push('./login');
})
Run Code Online (Sandbox Code Playgroud)
带有护照代码的快递
app.use('/', express.static(__dirname + '/../client'));
app.use(cors({credentials:true, origin:"http://localhost:3000"}));
app.use(logger('dev'));
app.use(busboyBodyParser({ multi : true }));
//app.use(bodyparser());
app.use(bodyparser.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieparser("password"));
app.use(session({
secret : "password",
resave : false,
saveUninitialized : true ,
cookie: {
path: '/',
httpOnly: true,
secure: false,
maxAge: …Run Code Online (Sandbox Code Playgroud)