【发布时间】:2018-08-29 18:47:27
【问题描述】:
我有这个中间件,需要让当前用户在 apollo 服务器的上下文中设置它
app.use(async (req, res, next)=>{
const token = req.headers['authorization'];
if(token !== "null"){
try {
const currentUser = await jwt.verify(token, process.env.SECRET)
req.currentUser = currentUser;
} catch (error) {
console.log(error);
}
}
next()
})
并且需要在上下文中设置当前用户
const SERVER = new ApolloServer({
schema,
context:{
currentUser //need to set this current user
}
})
SERVER.applymiddleware({app})
【问题讨论】: