【问题标题】:Can't get express-graphql to pass along passport user field无法让 express-graphql 传递护照用户字段
【发布时间】:2017-02-19 15:29:56
【问题描述】:

我的快速设置如下所示:

// 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);

除了 req.user 在我的 graphql 解析器方法中始终是 undefined 之外,Exerything 可以工作。添加到 apiRouter 的所有其他路由都可以看到 req.user 就好了。请求对象上的其他属性,如 req.db 无处不在。就好像graphql中间件在passport中间件之前执行一样,但我的理解是先声明的中间件也先执行。

【问题讨论】:

  • 如果它返回 null 而不是 undefined,那肯定表明该值发生了某些事情。您确定后端具有与此特定响应关联的用户值吗? null 是它从某个地方返回的值,否则它将是 undefined
  • 对不起,我应该说未定义。
  • 我们需要查看您的代码处理传递req 参数的请求。这绝对不是通过用户,但我在graphql 包中看不到任何会干扰这一点的东西。
  • 好吧,我的护照 LoginStrategy 可以在 github 中找到,如果你想看的话:github.com/viridia/klendathu/blob/apollo/server/actions/auth.js。我的 deserializeUser 函数没有做任何特别的事情。有趣的是,如果我在 graphql 回调中添加一个 console.log 语句(如上所示),req.user 甚至在它到达 graphql 之前就未定义。然而,它在其他路线中并非未定义。

标签: node.js express middleware graphql passport.js


【解决方案1】:

原来客户端的 fetch() 缺少“credentials: 'same-origin'” 选项,因此没有向下发送 cookie,这导致护照中间件没有设置用户字段。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    相关资源
    最近更新 更多