jaredhanson / passport

Simple, unobtrusive authentication for Node.js.

Home Page:https://www.passportjs.org?utm_source=github&utm_medium=referral&utm_campaign=passport&utm_content=about

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

profile-object-dosen't include user email

lokmanzeddoun opened this issue · comments

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