Node.js 使用MERN的passport jwt不显示cookie
我正在尝试使用nodejs实现jwt身份验证,并通过以下方式进行反应。在这里,他们使用了反应挂钩,但我希望在类中实现它,而不使用redux。但是我不知道如何使用axios在前端接收cookie我当前的代码是 后端:- user.js }) 前端:- login.jsNode.js 使用MERN的passport jwt不显示cookie,node.js,authentication,passport.js,mern,express-jwt,Node.js,Authentication,Passport.js,Mern,Express Jwt,我正在尝试使用nodejs实现jwt身份验证,并通过以下方式进行反应。在这里,他们使用了反应挂钩,但我希望在类中实现它,而不使用redux。但是我不知道如何使用axios在前端接收cookie我当前的代码是 后端:- user.js }) 前端:- login.js 我现在正准备对此进行研究,所以不确定,但我发现您需要在index.js(React部分)中设置以下内容: 就这样,cookie应该保存在浏览器中 看看这里,它可能会有帮助: userRouter.post('/login',pass
我现在正准备对此进行研究,所以不确定,但我发现您需要在index.js(React部分)中设置以下内容: 就这样,cookie应该保存在浏览器中 看看这里,它可能会有帮助:
userRouter.post('/login',passport.authenticate('local',{session : false}),(req,res)=>{
if(req.isAuthenticated()){
const {_id,username,role} = req.user;
const token =signToken(_id);
res.cookie('access_token',token,{httpOnly:true,sameSite:true});
res.status(200).json({isAuthenticated :true,user : {username,role}});
}
onSubmit(e){
e.preventDefault();
const user={
username:this.state.username,
password:this.state.password
}
console.log(user);
axios.post('http://localhost:5000/user/login',user)
.then(res=>{
console.log(res);
if(res.data.isAuthenticated){
console.log("authenticated")
}
})
}
axios.defaults.withCredentials = true;