【问题标题】:Wrapping passport.authenticate *callback* inside a function doesn't work在函数内包装 passport.authenticate *callback* 不起作用
【发布时间】: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


    【解决方案1】:

    This 为我指明了正确的方向。

    作品:

    const google_callback = async (ctx, next) => {
        await passport.authenticate('google',
            {
                successRedirect: '/auth/success',
                failureRedirect: '/auth/fail',
                failureFlash: true,
            }
        )(ctx, next);
    };
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 2020-11-14
      • 2017-04-18
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多