将Passport Google策略身份验证方法从路线移动到控制器中断

时间:2019-05-07 08:16:24

标签: javascript node.js express oauth-2.0 passport.js

我正在通过基于Node.js的Express应用程序使用Passport的“ passport-google-oauth”策略来提供身份验证。当我在路由文件中包含如下逻辑时,这非常有用:

router.route('/auth/google')
    .get(passport.authenticate('google', {
        scope: ['https://www.googleapis.com/auth/userinfo.profile']
    });
);

当我将所有内容移到控制器中以使其更易于管理时,它停止工作。具体来说,不会引发任何错误。 GET请求成功,并且debug方法输出字符串,但是随后它将永远加载,并且我没有被重定向到Google页面来选择用于登录的帐户。有什么想法在这里发生什么,我需要改变什么才能使其像美酒一样流动?

// route file
router.route('/auth/google')
    .get(backendController.googleAuthenticate);

// controller file
exports.googleAuthenticate = () => {
    debug('attempting Google authentication');
    passport.authenticate('google', {
        scope: ['https://www.googleapis.com/auth/userinfo.profile'],
        failWithError: true
    });
}

0 个答案:

没有答案