无法让 express-graphql 传递护照用户字段
Can't get express-graphql to pass along passport user field
我的快速设置如下所示:
// Set up router for the /api route.
const apiRouter = express.Router();
// Passport auth only applies to /api endpoints, not static resources
// or js bundles.
apiRouter.use(passport.initialize());
// (more passport initialization omitted for clarity)
// (other routes are added here, also omitted)
// Set up the graphql middleware
apiRouter.use('/gql', graphql(req => ({
schema,
rootValue: { db: req.db, user: req.user, req },
}));
// Now add the router to our app
app.use('/api', apiRouter);
Exerything 有效,除了 req.user
在我的 graphql 解析器方法中总是 undefined
。添加到 apiRouter 的所有其他路由都可以看到 req.user
就好了。请求对象上的其他属性,如 req.db
无处不在。这几乎就像是 graphql 中间件在 passport 中间件之前执行,但我的理解是先声明的中间件也先执行。
结果是客户端的 fetch() 缺少 "credentials: 'same-origin'" 选项,因此没有发送 cookie,这导致 passport 中间件没有设置用户字段。
我的快速设置如下所示:
// Set up router for the /api route.
const apiRouter = express.Router();
// Passport auth only applies to /api endpoints, not static resources
// or js bundles.
apiRouter.use(passport.initialize());
// (more passport initialization omitted for clarity)
// (other routes are added here, also omitted)
// Set up the graphql middleware
apiRouter.use('/gql', graphql(req => ({
schema,
rootValue: { db: req.db, user: req.user, req },
}));
// Now add the router to our app
app.use('/api', apiRouter);
Exerything 有效,除了 req.user
在我的 graphql 解析器方法中总是 undefined
。添加到 apiRouter 的所有其他路由都可以看到 req.user
就好了。请求对象上的其他属性,如 req.db
无处不在。这几乎就像是 graphql 中间件在 passport 中间件之前执行,但我的理解是先声明的中间件也先执行。
结果是客户端的 fetch() 缺少 "credentials: 'same-origin'" 选项,因此没有发送 cookie,这导致 passport 中间件没有设置用户字段。