Skip to content

profile-object-dosen't include user email #1028

Open
@lokmanzeddoun

Description

@lokmanzeddoun

passport-google-oauth20?

The email isn't returned in the Passport Google OAuth 2.0 strategy.
Detailed Description
I'm using the Passport Google OAuth 2.0 strategy to authenticate users in my application. However, when users authorize my application, the email isn't included in the profile object returned by Google. This is problematic as I require access to the user's email address for my application's functionality.
here is my code below

// 1-Google

passport.use(
	new GoogleStrategy(
		{
			clientID: process.env.CLIENT_ID,
			clientSecret: process.env.CLIENT_SECRET,
			callbackURL: process.env.GOOGLE_CALLBACK_URL,
		},
		async function (accessToken, refreshToken, profile, done) {
			try {
				console.log(profile);
				const [user, created] = await models.User.findOrCreate({
					where: { googleId: profile.id },
					defaults: {
						username: profile.displayName,
						email: profile.emails ? profile.emails[0].value : null, // Check if emails array exists
						profilePicture: profile.photos ? profile.photos[0].value : null, // Check if photos array exists
					},
				});

				return done(null, user); // Return the user instance
			} catch (error) {
				return done(error); // Pass any error to the done callback
			}
		}
	)
);
passport.serializeUser((user, done) => {
	done(null, user);
});

passport.deserializeUser((user, done) => {
	done(null, user);
});
router.get(
	"/google",
	passport.authenticate("google", { scope: ["profile", "email"] })
);
router.get(
	"/google/callback",
	passport.authenticate("google", { failureRedirect: "/signIn" }),
	function (req, res) {
		// Successful authentication, redirect home.
		res.redirect("/");
	}
);

the profile object dosen't include the emails property even when i make the email in scope

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions