【发布时间】:2022-11-21 22:01:25
【问题描述】:
我有一个自定义身份验证中间件:
export default async (req: Request, res: Response, next: NextFunction) => {
try {
const identity = await getIdentity(req);
if (!identity) {
throw new HttpError(401, 'Could not authenticate', [])
} else {
//Question: where to store the identity information so route middleware
// have access to it?
//@ts-ignore
req.identity = { hello: 'world' }
console.log('should have added identity')
next()
}
} catch {
throw new HttpError(500, 'Unknown validation error', [])
}
}
也许我的设计是错误的,但用户将发送自定义标头,这些标头将通过第三方服务进行身份验证以获取此用户/请求的身份详细信息。
【问题讨论】:
-
我的意思是……那应该行得通吗?
-
@AKX 我可以确认它有效但想知道是否只是改变请求是可行的方法,如果未来的 express 实现将在请求中使用身份怎么办?
标签: javascript node.js express authentication