Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

javascript - Express-session does not set cookie in Next.js app with Apollo client

I have this Next.js with Apollo client, and trying to authenticate with express-sesssion.

This is configs for cors and session:

...

const app = express()
if (isProduction) app.set('trust proxy', 1) // trust first proxy

app.use(cors({
  origin: true,
  credentials: true,
}))

const RedisStore = connectRedis(session)
app.use(session({
  store: new RedisStore({client: redis}),
  secret: 'foobar',
  resave: false,
  saveUninitialized: true,
  cookie: {
    maxAge: +COOKIE_MAX_AGE,
    httpOnly: true,
    secure: isProduction,
  },
}))

...

It does set cookie in Postman, but it does not set any cookie in Next.js app (tested with chromium).

This is the apolloClient config from Next.js:

...

function createApolloClient() {
  return new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: new HttpLink({
      uri: 'http://localhost:8000/graphql', // Server URL (must be absolute)
      // credentials: 'include', // Additional fetch() options like `credentials` or `headers`. Default is `same-origin`
    }),
    cache: new InMemoryCache(),
  })
}

...

Apollo said I should set credentials: 'include' if front and back are not same site. But it throws Unhandled Runtime Error Error: Failed to fetch if I set it.

Please help me, what I am doing wrong?

Stack: apollo-server-express, express, express-session, connect-redis, ioredis, next.js, @apollo/client


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...