【问题标题】:Express cookie parser is not creating cookies in productionExpress cookie 解析器未在生产中创建 cookie
【发布时间】:2020-06-28 09:08:36
【问题描述】:

我正在使用 graphql-yoga,并且正在编写访问 Microsoft Graph API 的 cookie。在前端,我有一个安装了 NextJS 的 apollo 客户端,它运行良好……正在开发中。当我部署服务器时,前端根本无法识别 cookie。在我的阅读中,我认为这与 NextJS 被服务器渲染有关(即使当我运行下一个构建时它说静态构建......)我确定问题出在此处(我将 cmets 留在里面,以显示我尝试将凭据设置为“包含”的所有地方)

export default function createApolloClient(initialState, ctx) {
  // The `ctx` (NextPageContext) will only be present on the server.
  // use it to extract auth headers (ctx.req) or similar.
  return new ApolloClient({
    ssrMode: Boolean(ctx),
    link: new HttpLink({
      fetch,
      uri: process.env.NODE_ENV === 'development' ? endpoint : prodEndpoint,
      credentials: 'include', // Additional fetch() options like `credentials` or `headers`
      fetchOptions: {
        credentials: 'include',
      },
      // request: operation => {
      //   operation.setContext({
      //     fetchOptions: {
      //       credentials: 'include',
      //     },
      //   });
      // },
    }),
    connectToDevTools: true,
    // credentials: 'include',
    cache: new InMemoryCache().restore(initialState),
  });
}

其他答案都涉及 CORS,但我在我的 GraphQL-Server 上设置了 CORS:

const opts = {
      debug: process.env.NODE_ENV === 'development',
      cors:
        process.env.NODE_ENV === 'development'
          ? {
              credentials: true,
              origin: ['http://localhost:3000'],
            }
          : {
              credentials: true,
              origin: [
                '...'
              ],
            },
    };
server.start(opts, () =>
      console.log('Playground is running on http://localhost:4000'),
    );

谁能指出我正确的方向?我查看前端的 ApolloClient 部分是否正确?提前致谢。

【问题讨论】:

    标签: next.js apollo-client express-graphql


    【解决方案1】:

    这是盯着我的脸,但他们的警告在控制台中被淹没了。 Cookie 需要在 Chrome 中设置。

    {
      ...,
      sameSite: false,
      secure: true
    }
    

    控制台有这些链接来提供洞察力:

    https://www.chromestatus.com/feature/5088147346030592

    https://www.chromestatus.com/feature/5633521622188032

    这是 Chrome 中最近的一个变化,我只是意识到有区别,因为我在 Firefox 中随机打开了我的网站,并且它起作用了。

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 2021-02-04
      • 2022-01-18
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2017-06-04
      相关资源
      最近更新 更多