Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,21 @@ passport.use(
},
async (req, accessToken, refreshToken, profile, done) => {
try {
const emailEntries = profile?.emails || profile?._json?.emails || [];
const primaryEmail = (
emailEntries.find(
(email) => typeof email?.value === 'string' && email.value.trim()
) || {}
).value;

if (!primaryEmail) {
return done(null, false, {
msg: 'Google account does not provide an email address.'
});
}

const existingUser = await User.findOne({
google: profile._json.emails[0].value
google: primaryEmail
}).exec();

if (existingUser) {
Expand All @@ -258,18 +271,16 @@ passport.use(
return done(null, existingUser);
}

const primaryEmail = profile._json.emails[0].value;

if (req.user) {
if (!req.user.google) {
req.user.google = profile._json.emails[0].value;
req.user.google = primaryEmail;
req.user.tokens.push({ kind: 'google', accessToken });
req.user.verified = User.EmailConfirmation().Verified;
}
await req.user.save();
return done(null, req.user);
}
let username = profile._json.emails[0].value.split('@')[0];
let username = primaryEmail.split('@')[0];
const existingEmailUser = await User.findByEmail(primaryEmail);
const existingUsernameUser = await User.findByUsername(username, {
caseInsensitive: true
Expand All @@ -285,14 +296,16 @@ passport.use(
return done(null, false, { msg: accountSuspensionMessage });
}
existingEmailUser.email = existingEmailUser.email || primaryEmail;
existingEmailUser.google = profile._json.emails[0].value;
existingEmailUser.google = primaryEmail;
existingEmailUser.username = existingEmailUser.username || username;
existingEmailUser.tokens.push({
kind: 'google',
accessToken
});
existingEmailUser.name =
existingEmailUser.name || profile._json.displayName;
existingEmailUser.name ||
profile.displayName ||
profile._json?.displayName;
existingEmailUser.verified = User.EmailConfirmation().Verified;

await existingEmailUser.save();
Expand All @@ -301,10 +314,10 @@ passport.use(

const user = new User();
user.email = primaryEmail;
user.google = profile._json.emails[0].value;
user.google = primaryEmail;
user.username = username;
user.tokens.push({ kind: 'google', accessToken });
user.name = profile._json.displayName;
user.name = profile.displayName || profile._json?.displayName;
user.verified = User.EmailConfirmation().Verified;

await user.save();
Expand Down