Skip to content

[#1910] Fix GitHub login to work with new passport-github2 package #1934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ passport.use(
clientSecret: process.env.GITHUB_SECRET,
callbackURL: '/auth/github/callback',
passReqToCallback: true,
scope: ['user:email']
scope: ['user:email'],
allRawEmails: true
},
(req, accessToken, refreshToken, profile, done) => {
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
Expand All @@ -132,8 +133,18 @@ passport.use(
}
req.user.save((saveErr) => done(null, req.user));
} else {
User.findByEmail(emails, (findByEmailErr, existingEmailUser) => {
if (existingEmailUser) {
User.findAllByEmails(emails, (findByEmailErr, existingEmailUsers) => {
if (existingEmailUsers.length) {
let existingEmailUser;
// Handle case where user has made multiple p5.js Editor accounts,
// with emails that are connected to the same GitHub account
if (existingEmailUsers.length > 1) {
existingEmailUser = existingEmailUsers.find(
(u) => (u.email = primaryEmail)
);
} else {
[existingEmailUser] = existingEmailUsers;
}
existingEmailUser.email = existingEmailUser.email || primaryEmail;
existingEmailUser.github = profile.id;
existingEmailUser.username =
Expand Down
17 changes: 17 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ userSchema.statics.findByEmail = function findByEmail(email, cb) {
return this.findOne(query).collation({ locale: 'en', strength: 2 }).exec(cb);
};

/**
*
* Queries User collection by emails and returns all Users that match.
*
* @param {string[]} emails - Array of email strings
* @callback [cb] - Optional error-first callback that passes User document
* @return {Promise<Object>} - Returns Promise fulfilled by User document
*/
userSchema.statics.findAllByEmails = function findAllByEmails(emails, cb) {
const query = {
email: { $in: emails }
};
// Email addresses should be case-insensitive unique
// In MongoDB, you must use collation in order to do a case-insensitive query
return this.find(query).collation({ locale: 'en', strength: 2 }).exec(cb);
};

/**
*
* Queries User collection by username and returns one User document.
Expand Down