【发布时间】:2023-03-16 23:45:01
【问题描述】:
这与Wrapping passport.authenticate inside a function doesn't work密切相关。
我们使用Koa 而不是Express。因此,我将(req, res, next) 替换为(ctx, next)。它在最初的 oauth2 调用中运行良好,但如果将回调包装在函数中,则会引发错误。
作品:
router.get('/auth/google/callback', passport.authenticate('google',
{
successRedirect: '/auth/success',
failureRedirect: '/auth/fail',
failureFlash: true,
}));
失败:
const google_callback = (ctx, next) => {
passport.authenticate('google',
{
successRedirect: '/auth/success',
failureRedirect: '/auth/fail',
failureFlash: true,
}
)(ctx, next);
};
router.get('/auth/google/callback', google_callback);
错误信息是:
Error: Can't set headers after they are sent.
【问题讨论】:
标签: passport.js koa