【问题标题】:Cookies not set using apollo-client, apollo-server and express-session未使用 apollo-client、apollo-server 和 express-session 设置 Cookie
【发布时间】:2021-07-31 06:47:39
【问题描述】:

我正在尝试使用 express-session 在登录时初始化 cookie,但它似乎只在从同一个站点请求时起作用。

阿波罗客户端:

 const endPoint = createUploadLink({
    uri: 'http://localhost:4000/graphql'
})

const client  = new ApolloClient({
   link: endPoint,
   credentials: 'include',
   cache: new InMemoryCache(),
 })

阿波罗服务器:

 const corsOptions = {
  origin: "http://localhost:3000",
  credentials: true,
  allowedHeaders: ['Content-Type', 'Authorization']
}
app.use(session({
  name: 'foo',
  secret: 'bar',
  resave: false,
  saveUninitialized: false,
  cookie: {
    maxAge: 360000,
    sameSite: 'none',
    httpOnly: true
  }
}))

const apolloServer = new ApolloServer({
  typeDefs,
  resolvers,
  playground: {
    settings: {
      'request.credentials': 'include'
    }
  },
  cors: corsOptions

});

现在,如果通过 graphql playground 发出请求,则设置 cookie,但如果从 localhost:3000 发出登录请求并且会话初始化,则不设置 cookie。

请问我在这里失踪了。

【问题讨论】:

    标签: reactjs cookies apollo-client apollo-server express-session


    【解决方案1】:
    const client = new ApolloClient({
      link: authLink.concat(httpLink),
      cache,
    
      fetchOptions: {
        credentials: "include",
      }
    })
    

    它对我有用...试试这个

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2020-08-08
      • 2023-02-25
      • 1970-01-01
      • 2021-01-15
      • 2021-02-05
      • 2021-04-09
      • 2022-07-11
      • 1970-01-01
      相关资源
      最近更新 更多