无法在我的浏览器的 cookie 中存储 json 网络令牌
Not able to store json web token in cookies in my browser
所以我正在学习使用 cors 在 MERN 项目中对用户登录进行 JWT 身份验证 npm.I 我能够生成 jwt 并将其存储在数据库中,但是我看不到我的 cookie正在存储在浏览器中。
这是我的 generateAuthToken() 函数,存在于 express 服务器的模式文件中,我在这里生成一个 jwt 并返回它 -
userSchema.methods.generateAuthToken = async function(){
try{
//generate token
const token = jwt.sign({_id:this._id.toString()} , process.env.SECRET_KEY);
//stored the generated token in our document
this.tokens = this.tokens.concat({token : token});
//saved the changes made in the document
await this.save();
//returning the token generated when called
return token;
}catch(err){
console.log(err);
}
}
并在 index.js 中调用 generateAuthtoken() 函数,出现在 express server.The 中,返回的令牌存储在 'token' -
中
const token = await userLogin.generateAuthToken();
//console.log(token);
//storing logged in tokens inside cookies
res.cookie("logintoken" , token , {
expires : new Date(Date.now() + 60000), //60sec from the time we store the token inside the cookie
httpOnly : true
});
并且在前端反应代码中,我在 fetch api 中发送凭证 header ,它调用我的 express 服务器中存在的登录路由 4000 端口 -
const response = await fetch('http://localhost:4000/login',{
method : "POST",
credentials: 'include',
headers : {
"Content-Type" : "application/json"
},
body : JSON.stringify({
email : email,
password : password
})
});
我在我的快速服务器中使用 cors npm 包处理 cross-region 请求 -
const cors = require('cors');
app.use(cors());
在我的浏览器控制台中,我遇到了这个错误 -
但是当我检查我的浏览器时,cookie 没有存储在存储器中 -
总而言之,我正在生成一个 jwt 并将其返回到 function.Then 中,将返回的令牌存储在名为 'logintoken' 的 cookie 中。但是该 cookie 并未存储在浏览器中。
我正在研究这个并且能够找到一些线程说明我们还必须在 cors 中定义一些 headers 以便浏览器能够存储 cookie.But 我不是能够弄清楚如何添加以及实现 this.I 需要哪些 header 之前使用 cookies & jwt 工作过,但这是我第一次学习 react 、 express & cors npm.Thank 你的回应。
这是我当前应用程序的代码片段
app.use(
cors({
credentials: true,
origin: `http://${process.env.REACT_APP_SERVER_HOST}:${process.env.PORT}`,
})
);
所以我正在学习使用 cors 在 MERN 项目中对用户登录进行 JWT 身份验证 npm.I 我能够生成 jwt 并将其存储在数据库中,但是我看不到我的 cookie正在存储在浏览器中。
这是我的 generateAuthToken() 函数,存在于 express 服务器的模式文件中,我在这里生成一个 jwt 并返回它 -
userSchema.methods.generateAuthToken = async function(){
try{
//generate token
const token = jwt.sign({_id:this._id.toString()} , process.env.SECRET_KEY);
//stored the generated token in our document
this.tokens = this.tokens.concat({token : token});
//saved the changes made in the document
await this.save();
//returning the token generated when called
return token;
}catch(err){
console.log(err);
}
}
并在 index.js 中调用 generateAuthtoken() 函数,出现在 express server.The 中,返回的令牌存储在 'token' -
中const token = await userLogin.generateAuthToken();
//console.log(token);
//storing logged in tokens inside cookies
res.cookie("logintoken" , token , {
expires : new Date(Date.now() + 60000), //60sec from the time we store the token inside the cookie
httpOnly : true
});
并且在前端反应代码中,我在 fetch api 中发送凭证 header ,它调用我的 express 服务器中存在的登录路由 4000 端口 -
const response = await fetch('http://localhost:4000/login',{
method : "POST",
credentials: 'include',
headers : {
"Content-Type" : "application/json"
},
body : JSON.stringify({
email : email,
password : password
})
});
我在我的快速服务器中使用 cors npm 包处理 cross-region 请求 -
const cors = require('cors');
app.use(cors());
在我的浏览器控制台中,我遇到了这个错误 -
但是当我检查我的浏览器时,cookie 没有存储在存储器中 -
总而言之,我正在生成一个 jwt 并将其返回到 function.Then 中,将返回的令牌存储在名为 'logintoken' 的 cookie 中。但是该 cookie 并未存储在浏览器中。
我正在研究这个并且能够找到一些线程说明我们还必须在 cors 中定义一些 headers 以便浏览器能够存储 cookie.But 我不是能够弄清楚如何添加以及实现 this.I 需要哪些 header 之前使用 cookies & jwt 工作过,但这是我第一次学习 react 、 express & cors npm.Thank 你的回应。
这是我当前应用程序的代码片段
app.use(
cors({
credentials: true,
origin: `http://${process.env.REACT_APP_SERVER_HOST}:${process.env.PORT}`,
})
);