Skip to content

Updated passport handles expected errors as exceptions #3049

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

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 9 additions & 13 deletions server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import User from '../models/user';
const accountSuspensionMessage =
'Account has been suspended. Please contact privacy@p5js.org if you believe this is an error.';

const accountLinkingErrorMessage =
'Account is already linked to another account.';

Comment on lines +16 to +18
Copy link
Collaborator

@lindapaiste lindapaiste Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this. I'm going to close this PR and go with #3050 instead because I don't like this particular change. IMO we want to keep the name of the service in there.

function generateUniqueUsername(username) {
const adj =
friendlyWords.predicates[
Expand Down Expand Up @@ -123,18 +126,15 @@ passport.use(
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
if (existingUser) {
if (req.user && req.user.email !== existingUser.email) {
done(
new Error('GitHub account is already linked to another account.')
);
done(null, false, { msg: accountLinkingErrorMessage });
return;
} else if (existingUser.banned) {
done(new Error(accountSuspensionMessage));
done(null, false, { msg: accountSuspensionMessage });
return;
}
done(null, existingUser);
return;
}

const emails = getVerifiedEmails(profile.emails);
const primaryEmail = getPrimaryEmail(profile.emails);

Expand All @@ -159,7 +159,7 @@ passport.use(
[existingEmailUser] = existingEmailUsers;
}
if (existingEmailUser.banned) {
done(new Error(accountSuspensionMessage));
done(null, false, { msg: accountSuspensionMessage });
return;
}
existingEmailUser.email = existingEmailUser.email || primaryEmail;
Expand Down Expand Up @@ -218,14 +218,10 @@ passport.use(
(findByGoogleErr, existingUser) => {
if (existingUser) {
if (req.user && req.user.email !== existingUser.email) {
done(
new Error(
'Google account is already linked to another account.'
)
);
done(null, false, { msg: accountLinkingErrorMessage });
return;
} else if (existingUser.banned) {
done(new Error(accountSuspensionMessage));
done(null, false, { msg: accountSuspensionMessage });
return;
}
done(null, existingUser);
Expand Down Expand Up @@ -256,7 +252,7 @@ passport.use(
// then, append a random friendly word?
if (existingEmailUser) {
if (existingEmailUser.banned) {
done(new Error(accountSuspensionMessage));
done(null, false, { msg: accountSuspensionMessage });
return;
}
existingEmailUser.email =
Expand Down