Skip to content

Commit e18bf8e

Browse files
committed
Merge branch 'develop' into chore/ban-users
2 parents b8d4984 + 57823ef commit e18bf8e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

server/config/passport.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ passport.use(
117117
clientSecret: process.env.GITHUB_SECRET,
118118
callbackURL: '/auth/github/callback',
119119
passReqToCallback: true,
120-
scope: ['user:email']
120+
scope: ['user:email'],
121+
allRawEmails: true
121122
},
122123
(req, accessToken, refreshToken, profile, done) => {
123124
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
@@ -149,8 +150,18 @@ passport.use(
149150
}
150151
req.user.save((saveErr) => done(null, req.user));
151152
} else {
152-
User.findByEmail(emails, (findByEmailErr, existingEmailUser) => {
153-
if (existingEmailUser) {
153+
User.findAllByEmails(emails, (findByEmailErr, existingEmailUsers) => {
154+
if (existingEmailUsers.length) {
155+
let existingEmailUser;
156+
// Handle case where user has made multiple p5.js Editor accounts,
157+
// with emails that are connected to the same GitHub account
158+
if (existingEmailUsers.length > 1) {
159+
existingEmailUser = existingEmailUsers.find(
160+
(u) => (u.email = primaryEmail)
161+
);
162+
} else {
163+
[existingEmailUser] = existingEmailUsers;
164+
}
154165
existingEmailUser.email = existingEmailUser.email || primaryEmail;
155166
existingEmailUser.github = profile.id;
156167
existingEmailUser.username =

server/models/user.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ userSchema.statics.findByEmail = function findByEmail(email, cb) {
208208
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
209209
};
210210

211+
/**
212+
*
213+
* Queries User collection by emails and returns all Users that match.
214+
*
215+
* @param {string[]} emails - Array of email strings
216+
* @callback [cb] - Optional error-first callback that passes User document
217+
* @return {Promise<Object>} - Returns Promise fulfilled by User document
218+
*/
219+
userSchema.statics.findAllByEmails = function findAllByEmails(emails, cb) {
220+
const query = {
221+
email: { $in: emails }
222+
};
223+
// Email addresses should be case-insensitive unique
224+
// In MongoDB, you must use collation in order to do a case-insensitive query
225+
return this.find(query).collation({ locale: 'en', strength: 2 }).exec(cb);
226+
};
227+
211228
/**
212229
*
213230
* Queries User collection by username and returns one User document.

0 commit comments

Comments
 (0)