1
在我送令牌每一個請求,並檢查它在快遞中間件如何編寫中間件graphql將每一個解析器之前調用
app.use(async (req, res, next) => {
const authorization = req.headers.authorization;
let token = null;
let user;
if (authorization) {
try {
token = jwt.verify(authorization, config.secret);
} catch (e) {
// dont work
throw new GraphQLError({ message: 'token damaged' });
}
if (token) {
const { _id } = token;
user = await User.findOne({ _id });
}
if (user) {
req.user = user;
}
}
next();
});
令牌可能會被損壞,而我做的檢查:
try {
token = jwt.verify(authorization, config.secret);
} catch (e) {
throw new GraphQLError({ message: 'token damaged' });
}
所以我需要發送到客戶端應用程序快速錯誤,但它不工作,正如所料, 是否有任何選項來創建graphql中間件在調用每個解析器之前接受請求參數?現在,如果我想拋出損壞的令牌錯誤,我需要在每個解析器中寫入檢查?
也許這可以幫助你https://stackoverflow.com/questions/18700729/how-to-use-the-middleware-在進入每條路線之前檢查授權 –
https://github.com/DubFriend/graphql-resolve –