2016-10-11 30 views
0

我快速设置看起来是这样的:不能得到表达,graphql沿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作品除了在我的graphql解析方法是req.user总是undefined。所有添加到apiRouter的其他路由都可以看到req.user。请求对象上的其他属性如req.db无处不在。就好像graphql中间件在护照中间件之前执行,但我的理解是,首先声明的中间件也首先执行。

+0

如果它返回'null'而不是'undefined',那肯定表明这个值已经发生了。您确定后端具有与此特定响应关联的用户值吗? 'null'是它从某个地方返回的值,否则就是'undefined'。 – Dandy

+0

对不起,我应该说undefined。 – Talin

+0

我们需要看你的代码处理传递'req'参数的请求。这肯定不会传递给用户,但是我看不到'graphql'包中会干扰这个的任何内容。 – Dandy

回答

2

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