在Node.js Express REST API中处理passport.js时出错
我已经阅读了护照文件、战略文件和搜索论坛,但我没有找到这些问题的答案 描述/目标: 我正试图通过使用以下两种策略来保护我的Node.JS REST API和passport.JS:passport azure ad和passport headerAPI 当passport中出现错误时,我希望通过以下方式捕获并处理它:在Node.js Express REST API中处理passport.js时出错,node.js,passport.js,passport-azure-ad,Node.js,Passport.js,Passport Azure Ad,我已经阅读了护照文件、战略文件和搜索论坛,但我没有找到这些问题的答案 描述/目标: 我正试图通过使用以下两种策略来保护我的Node.JS REST API和passport.JS:passport azure ad和passport headerAPI 当passport中出现错误时,我希望通过以下方式捕获并处理它: 获取对错误对象的访问权限,以便我可以console.log、将其记录到数据库或执行其他逻辑 在对客户端的响应中返回自定义错误。(不仅仅像passport默认情况下那样) 它不能使用
return done('This is an error message'); // Results in a 500 returned with HTML body
return done('This is an error message', false, null); // Results in a 500 returned with HTML body
return done(null, false, 'This is an error message'); // Results in 401 with Unauthorized string body
设置:
我已经选择使用链中的第一个路由作为app.all(“*”),并在这里执行所有身份验证,这样我就不必到处实现passport中间件。
如果passport在验证前或验证后的策略中检测到任何问题,则根本不调用路由
app.all('*',
passport.authenticate(
[bearerStrategy, headerAPIKeyStrategy],
{
session: false
}
),
(req, res, next) => {
// THIS IS NOT GETTING RUN IF PASSPORT HAS DETECTED ANY ERRORS.
// If passport strategy and validation is successfull the returned validatet object is in req.user.
console.log(req.user);
// Just call next so the correct route is used, req.user will be used for authorization later.
next();
});
是否有任何方法设置此设置,以便可以访问和处理passport中检测到的错误
如果不是,passport真的应该用于生产吗?或者在Node.js中有更好的Azure AD身份验证替代方案吗?(MS示例推荐passport azure广告)
感谢您的回复