Javascript 来自不同来源的Cookie
给定以下用于突变的解析器:Javascript 来自不同来源的Cookie,javascript,cookies,graphql,jwt,samesite,Javascript,Cookies,Graphql,Jwt,Samesite,给定以下用于突变的解析器: 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')
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;
}
当代码是这样时,我得到以下警告:
与位于的跨站点资源关联的cookie设置时没有SameSite
属性。它已被阻止,因为Chrome现在只提供带有跨站点请求的cookie,如果它们设置为SameSite=None
和Secure
。您可以在应用程序>存储>cookies下查看开发人员工具中的cookies,并在和中查看更多详细信息
后台故事:前端部分是一个React应用程序,托管在timeline-React-pd.herokuapp.com上。后端部分是一个节点应用程序,托管在timeline-yoga-pd.herokuapp.com上
现在,当我将secure设置为true时,正如警告中所询问的,cookie头没有设置。我在网上发现,这是正常的
当我将sameSite设置为false时,它的行为就像它不存在一样(预期行为)。如果将sameSite设置为“none”或“none”,警告将消失,但会出现新的错误,说明GraphQL不接受值“none” 你试过将cookie的域设置为两个域中的顶级域herokuapp.com吗?我试过了。它不会给出任何错误或警告,但仍然不会设置cookie。您在这里使用的是什么库、客户端或框架?
SameSite=None
值相对较新,我注意到有几个位置验证参数,只接受较旧的Strict
和Lax
值。如果我知道你在使用什么,我可能会去更新它们以支持None
。我相信GraphQL不会接受它。我发现Express 4.17接受“无”。当我这样设置时,chrome停止发送警告,但是GraphQL出现了一个新的错误,它无法识别“none”。当它说它不接受“none”时,你能发布你得到的确切错误吗?特别是行号和文件名。我在GraphQL代码中没有看到任何关于SameSite
属性的内容,因此这感觉像是其他依赖项。