node.jsexpressgoogle-oauthpassport-google-oauth

I'm doing authentication using Google OAuth. How do I fetch the details of the user


I'm using Google Passport to do authentication.
But now I want some few extra details of the user such as profile image and gender.
So how do I fetch the profile image and gender of the user.

Helps are really appriciated.

exports.googlePassport = catchAsync(async (req, res, next) => {
    passport.use(new GoogleStrategy({
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        callbackURL: "http://localhost:8001/api/v1/user/google/callback",
        scope: ["profile", "email"]
    }, async (profile) => {
        const newUser = {
            googleId: profile.id,
            firstname: profile.name.givenName,
            lastname: profile.name.familyName,
            email: profile.emails[0].value
        };
    }));
})

Solution

  • Profile Image can be fetched but the gender can only be fetched if the user has given the permission in his google profile that gender can be accessed by everyone.

    exports.googlePassport = catchAsync(async (req, res, next) => {
    passport.use(new GoogleStrategy({
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        callbackURL: "http://localhost:8001/api/v1/user/google/callback",
        scope: ["profile", "email"]
    }, async (profile) => {
        const newUser = {
            googleId: profile.id,
            firstname: profile.name.givenName,
            lastname: profile.name.familyName,
            email: profile.emails[0].value,
            profileImage: profile.photos[0].value,
            gender: profile.gender
        };
    }));
    

    })