Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lokmanzeddoun opened this issue Jun 9, 2024 · 1 comment
Open

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

lokmanzeddoun opened this issue Jun 9, 2024 · 1 comment

Comments

@lokmanzeddoun
Copy link

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

@mail2jomy
Copy link

@lokmanzeddoun , did you resolve this issue. i'm facing the same..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants