为突变提供以下解析器:
async signin(parent, { name, password }, ctx, info) {
// Check if there is a user with the name
const user = await ctx.db.query.user({
where: { name }
})
if (!user) {
throw new Error('Name not found');
}
// Check if the password is correct
const valid = await bcrypt.compare(password, user.password);
if (!valid) {
throw new Error('Invalid password');
}
// Genereate the JWT
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
// Set the cookie with the token
ctx.response.cookie('token', token, {
httpOnly: true,
// secure: ...,
// sameSite: ...,
maxAge: 1000 * 60 * 60 * 24 * 365
});
return user;
}
当代码是这样时,我收到以下警告:
设置了与http://timetable-yoga-pd.herokuapp.com/处的跨站点资源关联的cookie,但未设置
SameSite
属性。它已被阻止,因为Chrome现在仅在跨站点请求中将Cookie设置为SameSite=None
和Secure
时才发送。您可以在Application> Storage> Cookies下的开发人员工具中查看Cookie,并在https://www.chromestatus.com/feature/5088147346030592和https://www.chromestatus.com/feature/5633521622188032上查看更多详细信息。
背景故事:前端部分是一个React应用,托管在timetable-react-pd.herokuapp.com上。后端部分是一个节点应用程序,托管在timetable-yoga-pd.herokuapp.com上。
现在,如警告中所述,当我将安全性设置为true时,未设置cookie标头。我在互联网上发现这很正常。
当我将sameSite设置为false时,它的作用就好像它不存在(预期行为)。如果将sameSite设置为“ none”或“ None”,则警告消失,但出现新的错误,表示GraphQL不接受值“ none”。