Skip to content

Commit 505cdc3

Browse files
committed
fix: add email validation for google oauth
1 parent bfe708c commit 505cdc3

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

server/config/passport.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ const getVerifiedEmails = (githubEmails) =>
124124
const getPrimaryEmail = (githubEmails) =>
125125
(lodash.find(githubEmails, { primary: true }) || {}).value;
126126

127+
/**
128+
* Get primary email from Google OAuth profile.
129+
* Returns the first email if available, or null if emails array is missing/empty.
130+
*/
131+
const getGooglePrimaryEmail = (googleEmails) => {
132+
if (
133+
!googleEmails ||
134+
!Array.isArray(googleEmails) ||
135+
googleEmails.length === 0
136+
) {
137+
return null;
138+
}
139+
return googleEmails[0]?.value || null;
140+
};
141+
127142
/**
128143
* Sign in with GitHub.
129144
*/
@@ -240,8 +255,18 @@ passport.use(
240255
},
241256
async (req, accessToken, refreshToken, profile, done) => {
242257
try {
258+
// Validate that emails array exists and has at least one element
259+
const primaryEmail = getGooglePrimaryEmail(profile._json?.emails);
260+
if (!primaryEmail) {
261+
return done(null, false, {
262+
msg:
263+
'Unable to retrieve email from Google account. ' +
264+
'Please ensure your Google account has an email address and try again.'
265+
});
266+
}
267+
243268
const existingUser = await User.findOne({
244-
google: profile._json.emails[0].value
269+
google: primaryEmail
245270
}).exec();
246271

247272
if (existingUser) {
@@ -258,18 +283,16 @@ passport.use(
258283
return done(null, existingUser);
259284
}
260285

261-
const primaryEmail = profile._json.emails[0].value;
262-
263286
if (req.user) {
264287
if (!req.user.google) {
265-
req.user.google = profile._json.emails[0].value;
288+
req.user.google = primaryEmail;
266289
req.user.tokens.push({ kind: 'google', accessToken });
267290
req.user.verified = User.EmailConfirmation().Verified;
268291
}
269292
await req.user.save();
270293
return done(null, req.user);
271294
}
272-
let username = profile._json.emails[0].value.split('@')[0];
295+
let username = primaryEmail.split('@')[0];
273296
const existingEmailUser = await User.findByEmail(primaryEmail);
274297
const existingUsernameUser = await User.findByUsername(username, {
275298
caseInsensitive: true
@@ -285,7 +308,7 @@ passport.use(
285308
return done(null, false, { msg: accountSuspensionMessage });
286309
}
287310
existingEmailUser.email = existingEmailUser.email || primaryEmail;
288-
existingEmailUser.google = profile._json.emails[0].value;
311+
existingEmailUser.google = primaryEmail;
289312
existingEmailUser.username = existingEmailUser.username || username;
290313
existingEmailUser.tokens.push({
291314
kind: 'google',
@@ -301,7 +324,7 @@ passport.use(
301324

302325
const user = new User();
303326
user.email = primaryEmail;
304-
user.google = profile._json.emails[0].value;
327+
user.google = primaryEmail;
305328
user.username = username;
306329
user.tokens.push({ kind: 'google', accessToken });
307330
user.name = profile._json.displayName;

0 commit comments

Comments
 (0)