0

给定以下突变解析器:

    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属性。SameSite=None它已被阻止,因为 Chrome 现在仅在使用和设置时才提供带有跨站点请求的 cookie Secure。您可以在应用程序>存储>Cookies 下的开发人员工具中查看 cookie,并在https://www.chromestatus.com/feature/5088147346030592https://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”。

4

0 回答 0