Jawad
Jawad

Reputation: 333

how to send user profile data on success redirect with passport?

I'm quite new to Node.js, and i'm learning authentication with passport, in my node application i have following lines which authenticates user with google and fires a callback after successful authentication,

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

i want to send the user profile data to the page registered with successRedirect, how can i do that? my passport strategy for authentication with google is like so

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

        }

    });

}));

Upvotes: 1

Views: 1585

Answers (1)

Aric Sangchat
Aric Sangchat

Reputation: 41

Im not sure what front end your using but if its React with Redux and if you have SSR you can preloadState with the req.user info by firing an action from your server. Here's documentation on that.

Or if you have session's setup you can make a get request when your front end loads and respond back with the req.user info.

Upvotes: 0

Related Questions