Passport.authenticate 在 Google Login 中为请求注入空对象



我正在使用Express + Passport为我的应用程序进行Google身份验证。

这是我的路线:

const authCallback = function(req, res) {
    if (req.user._id) {
        if (abc()) { return res.redirect('/app'); }
        res.redirect('/profile/' + req.user._id.toString());
    } else {
        res.redirect('/');
    }
};
app.get('/auth/google/callback', passport.authenticate('google', authCallback));

我收到错误无法读取函数中第 1 行 null 的属性"用户">authCallback

我尝试调试这个,但找不到解决方案。

如果需要更多代码,请告诉我。

auth 回调不应该有 req,res . 它返回function(err, user, info) { }):

  const authCallback = function(err, user,info) {
        if (user._id) {
            if (abc()) { return res.redirect('/app'); }
            res.redirect('/profile/' + user._id.toString());
        } else {
            res.redirect('/');
        }
    };
    app.get('/auth/google/callback', passport.authenticate('google', authCallback));

感谢Dinesh的回答。

这完全解决了问题。

app.get('/auth/google/callback', passport.authenticate('google'), authCallback);

而不是

app.get('/auth/google/callback', passport.authenticate('google', authCallback));

回调旨在传递给快速获取 API,但正在传递给护照 API。

最新更新