2017-03-10 24 views
0

这与Wrapping passport.authenticate inside a function doesn't work密切相关。包装passport.authenticate * callback *函数内部不起作用

我们使用Koa而不是Express。因此,我用(ctx, next)替换了(req, res, 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. 

回答

1

This我指出了正确的方向。

作品:

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