Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

})

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading