如何使用护照成功重定向发送用户个人资料数据?

时间:2017-10-04 22:04:29

标签: node.js reactjs google-authentication passport.js

我对Node.js很新,我正在学习使用护照进行身份验证,在我的节点应用程序中,我有以下行,用google验证用户并在成功验证后触发回调,

  // send to google to do the authentication
        app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] }));

        // the callback after google has authenticated the user
        app.get('/auth/oauth/callback',passport.authenticate('google', {successRedirect : 'http://127.0.0.1:1234/testbot',failureRedirect : '/'}));

我想将用户个人资料数据发送到使用successRedirect注册的页面,我该怎么做? 我与谷歌认证的护照策略是这样的

passport.use(new GoogleStrategy({
    clientID        : configAuth.googleAuth.clientID,
    clientSecret    : configAuth.googleAuth.clientSecret,
    callbackURL     : configAuth.googleAuth.callbackURL,
    passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)

},
function(req, token, refreshToken, profile, done) {
    console.log("Its here ");
    // asynchronous
    process.nextTick(function() {

        // check if the user is already logged in
        if (!req.user) {

            User.findOne({ 'UserName' : req.body.email }, function(err, user) {
                if (err)
                    return done(err);

                if (user) {

                    // if there is a user id already but no token (user was linked at one point and then removed)
                    if (!user.google.token) {
                        user.google.token = token;
                        user.google.name  = profile.displayName;
                        user.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email

                        user.save(function(err) {
                            if (err)
                                return done(err);

                            return done(null, user);
                        });
                    }

                    return done(null, user);
                } else {
                    var newUser          = new User();
                    newUser.UserName= (profile.emails[0].value || '').toLowerCase(); 
                    newUser.google.id    = profile.id;
                    newUser.google.token = token;
                    newUser.google.name  = profile.displayName;
                    newUser.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email

                    newUser.save(function(err) {
                        if (err)
                            return done(err);

                        return done(null, newUser);
                    });
                }
            });

        } else {
            // user already exists and is logged in, we have to link accounts
            var user               = req.user; // pull the user out of the session

            user.google.id    = profile.id;
            user.google.token = token;
            user.google.name  = profile.displayName;
            user.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email

            user.save(function(err) {
                if (err)
                    return done(err);

                return done(null, user);
            });

        }

    });

}));

1 个答案:

答案 0 :(得分:0)

我不确定您使用的前端是什么,但是如果它与Redux的React并且如果您有SSR,则可以通过从服务器触发操作来使用req.user信息预加载状态。 Here's documentation on that.

或者,如果您有会话设置,您可以在前端加载时发出请求请求,并使用req.user信息进行回复。