使用护照本地作为身份验证策略的快速应用程序中未受保护的端点

Unprotected endpoint in an express app that use passport-local as auth strategy

我想知道如何创建无需任何身份验证即可访问的端点(GET、POST 等...)。这是我的代码:

router.use(AuthenticationManager.ensureAuthenticated());

router.use('/', require('./index'));
router.use('/path-1', require('./path1'));
router.use('/path-2', require('./path2'));

所有端点都将享受Authentication Manager。如何仅在 ./path1./path2 内的某些端点禁用该身份验证管理器?

执行此操作的常规方法是在 AuthenticationManager 中间件之前定义这些端点:

router.use('/path-1/unprotected', require('./path1'));
router.use('/path-2/unprotected', require('./path2'));
router.use(AuthenticationManager.ensureAuthenticated());

这取决于 path1.jspath2.js 导出的内容是否按原样工作,或者您是否需要进行一些重写。

您可以使用此方法对特定端点使用身份验证:

app.post('/profile', AuthenticationManager.ensureAuthenticated , userController.getUserProfile);