使用Google Passport OAuth登录后,req.user未定义。

4

成功使用Google账户登录后,在回调请求中,req.user被设置为正确的用户,req.isAuthenticated()为true。太好了!

然而,任何后续请求都会将req.user设置为未定义。无论我做什么。

这个问题在passport-google-auth2passport-google-auth中都存在。

以下是我的index.js的样式:

app.set('views', './src/express/views');
app.set('view engine', 'jsx');
app.engine('jsx', expressReactViews.createEngine({ beautify: true }));

app.use(express.static('./dist'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use( bodyParser.urlencoded({
    extended: true
}));
app.use(session({
    secret: 'keyboard cat',
    proxy: true,
    resave: true,
    saveUninitialized: true,
    cookie: { secure: true }
}));
app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function(user, done) {
    // user is passed here correctly
    done(null, user.id);
});
passport.deserializeUser(function(userId, done) {
    db.connect(function(error, connection) {
        if(error) {
            done(error);
        }
        else {
            users.find(connection, userId, function (user) {
                connection.close();
                // user is populated here, don't worry :)
                done(null, user);
            });
        }
    });
});

passport.use(googleStrategy);

这就是 googleStrategy 的样子:

module.exports = new GoogleStrategy(
    {
        clientID: '[FAKE]',
        clientSecret: '[FAKE]',
        callbackURL: 'http://localhost:3000/auth/google/callback'
    },
    function(accessToken, refreshToken, profile, done) {
        db.connect((error, connection) => {
            if(error) {
                done(error);
            }
            else {
                users.findOrCreateFromGoogleProfile(connection, profile, (error, user) => {
                    connection.close();
                    done(error, user);
                });
            }
        });
    }
);

这是我的auth路由器的样子:
router.route('/google/callback').get(passport.authenticate('google', {
    failureRedirect: '/error'
}), function(req, res) {
    // here everything works. req.user is correctly set and req.isAuthenticated() is true
    res.redirect('/index');
});

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

module.exports = router;

我的操作有误吗?

2个回答

2

我希望我可以评论而不是直接回答这个问题。

你可以试着运行这个命令吗?

router.route('/google/callback').get(passport.authenticate('google', {
    failureRedirect: '/error'
}), function(req, res) {
    // here everything works. req.user is correctly set and req.isAuthenticated() is true

    req.session.save(function(err) {

        res.redirect('/index');
    });
});

由于似乎一切都正常工作,直到重定向之后,因此这可能会解决问题。


谢谢您花时间尝试,但是它没有起作用。我已经厌倦了调整属性却毫无结果 :( 有时候 Node 毫无帮助。 - Andre Pena
1
很抱歉听到这个消息,除此之外,代码看起来没问题。祝你好运! - eddyjs
1
我想到了一个可能有效的解决方案。由于似乎一切都能正常工作,直到重定向之后,我已经更新了我的答案,提供了另一个可能的解决方案。 - eddyjs
再次感谢 @edwinlin1987。但是它仍然不起作用。在这个特定的点上,req.user 是存在的,但在未来的请求中却不存在。我相信这与我的应用程序无法持久化会话或 cookie 有关。如果您知道如何检查它,我将不胜感激。 - Andre Pena
非常抱歉,我的反序列化方法即使对于有效的用户也返回了null。我感到非常尴尬,之前没有意识到这一点。感谢您花费时间。我给你点赞,这样你至少可以得到一些积分。 - Andre Pena
显示剩余2条评论

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接