简体   繁体   English

使用google-passport-oauth登录后,req.user未定义

[英]req.user is undefined after logging in with google-passport-oauth

After I successfully log in using a Google account, in the callback request, req.user is set to the right user and req.isAuthenticated() is true. 使用Google帐户成功登录后,在回调请求中, req.user设置为正确的用户, req.isAuthenticated()为true。 Great! 大!

However, any subsequent request will have req.user as undefined. 但是,任何后续请求都将req.user为undefined。 No matter what I do. 不管我做什么。

The problem occurs with both passport-google-auth2 and passport-google-auth . 使用passport-google-auth2passport-google-auth时会出现此问题。

This is how my index.js looks like: 这就是我的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);

And this is how googleStrategy looks like: 这就是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);
                });
            }
        });
    }
);

This is how my auth router looks like: 这是我的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;

Am I doing anything wrong? 我做错了吗?

I wish I could comment instead of directly answer this question. 我希望我能发表评论,而不是直接回答这个问题。

Can you try running this instead? 你可以试试这个吗?

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');
    });
});

Since everything seems to work until after the redirect, this might fix the issue. 由于在重定向之后一切似乎都有效,这可能会解决问题。

I would guess that the user info isn't being saved to the db. 我猜想用户信息没有保存到数据库中。

If you go to the following link and remove your app/site from the list of approved connected apps/sites, are you then able to log in once more? 如果您转到以下链接并从已批准的已连接应用/网站列表中删除您的应用/网站,那么您是否可以再次登录?

https://myaccount.google.com/security#connectedapps https://myaccount.google.com/security#connectedapps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM