Skip to content

Commit 57823ef

Browse files
authored
Merge pull request #1934 from processing/bug/github-login
[#1910] Fix GitHub login to work with new passport-github2 package
2 parents cad726c + b5b2e93 commit 57823ef

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
@@ -106,7 +106,8 @@ passport.use(
106106
clientSecret: process.env.GITHUB_SECRET,
107107
callbackURL: '/auth/github/callback',
108108
passReqToCallback: true,
109-
scope: ['user:email']
109+
scope: ['user:email'],
110+
allRawEmails: true
110111
},
111112
(req, accessToken, refreshToken, profile, done) => {
112113
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
@@ -132,8 +133,18 @@ passport.use(
132133
}
133134
req.user.save((saveErr) => done(null, req.user));
134135
} else {
135-
User.findByEmail(emails, (findByEmailErr, existingEmailUser) => {
136-
if (existingEmailUser) {
136+
User.findAllByEmails(emails, (findByEmailErr, existingEmailUsers) => {
137+
if (existingEmailUsers.length) {
138+
let existingEmailUser;
139+
// Handle case where user has made multiple p5.js Editor accounts,
140+
// with emails that are connected to the same GitHub account
141+
if (existingEmailUsers.length > 1) {
142+
existingEmailUser = existingEmailUsers.find(
143+
(u) => (u.email = primaryEmail)
144+
);
145+
} else {
146+
[existingEmailUser] = existingEmailUsers;
147+
}
137148
existingEmailUser.email = existingEmailUser.email || primaryEmail;
138149
existingEmailUser.github = profile.id;
139150
existingEmailUser.username =

server/models/user.js

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

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

0 commit comments

Comments
 (0)