问题描述
我有这个带有 Apollo 客户端的 Next.js,并尝试使用 express-sesssion
进行身份验证。
这是 cors
和 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,},}))
...
它确实在 Postman 中设置了 cookie,但它没有在 Next.js 应用程序中设置任何 cookie(用铬测试)。
这是 Next.js 中的 apolloClient
配置:
...
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 说如果正面和背面不是同一个站点,我应该设置 credentials: 'include'
。但是如果我设置它,它会抛出 Unhandled Runtime Error Error: Failed to fetch
。
我做错了什么?
堆栈:apollo-server-express、express、express-session、connect-redis、ioredis、next.js、@apollo/client
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)