Skip to content

Commit b81ebe0

Browse files
authored
Merge pull request #3107 from processing/fix/retry-addressing-errors
Fix/retry addressing errors
2 parents 8dc7b64 + 801cbb1 commit b81ebe0

File tree

6 files changed

+356
-443
lines changed

6 files changed

+356
-443
lines changed

server/config/passport.js

Lines changed: 152 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,30 @@ passport.use(
7070
* Authentificate using Basic Auth (Username + Api Key)
7171
*/
7272
passport.use(
73-
new BasicStrategy((userid, key, done) => {
74-
User.findByUsername(userid, (err, user) => {
75-
if (err) {
76-
done(err);
77-
return;
78-
}
73+
new BasicStrategy(async (userid, key, done) => {
74+
try {
75+
const user = await User.findByUsername(userid);
76+
7977
if (!user) {
80-
done(null, false);
81-
return;
78+
return done(null, false);
8279
}
80+
8381
if (user.banned) {
84-
done(null, false, { msg: accountSuspensionMessage });
85-
return;
82+
return done(null, false, { msg: accountSuspensionMessage });
8683
}
87-
user.findMatchingKey(key, (innerErr, isMatch, keyDocument) => {
88-
if (isMatch) {
89-
keyDocument.lastUsedAt = Date.now();
90-
user.save();
91-
done(null, user);
92-
return;
93-
}
94-
done(null, false, { msg: 'Invalid username or API key' });
95-
});
96-
});
84+
85+
const { isMatch, keyDocument } = await user.findMatchingKey(key);
86+
if (!isMatch) {
87+
return done(null, false, { message: 'Invalid API key' });
88+
}
89+
90+
keyDocument.lastUsedAt = Date.now();
91+
await user.save();
92+
return done(null, user);
93+
} catch (err) {
94+
console.error(err);
95+
return done(null, false, { msg: err });
96+
}
9797
})
9898
);
9999

@@ -128,20 +128,19 @@ passport.use(
128128
scope: ['user:email'],
129129
allRawEmails: true
130130
},
131-
(req, accessToken, refreshToken, profile, done) => {
132-
User.findOne({ github: profile.id }, (findByGithubErr, existingUser) => {
131+
async (req, accessToken, refreshToken, profile, done) => {
132+
try {
133+
const existingUser = await User.findOne({ github: profile.id });
134+
133135
if (existingUser) {
134136
if (req.user && req.user.email !== existingUser.email) {
135-
done(null, false, {
137+
return done(null, false, {
136138
msg: 'GitHub account is already linked to another account.'
137139
});
138-
return;
139140
} else if (existingUser.banned) {
140-
done(null, false, { msg: accountSuspensionMessage });
141-
return;
141+
return done(null, false, { msg: accountSuspensionMessage });
142142
}
143-
done(null, existingUser);
144-
return;
143+
return done(null, existingUser);
145144
}
146145

147146
const emails = getVerifiedEmails(profile.emails);
@@ -153,58 +152,63 @@ passport.use(
153152
req.user.tokens.push({ kind: 'github', accessToken });
154153
req.user.verified = User.EmailConfirmation.Verified;
155154
}
156-
req.user.save((saveErr) => done(null, req.user));
157-
} else {
158-
User.findAllByEmails(emails, (findByEmailErr, existingEmailUsers) => {
159-
if (existingEmailUsers.length) {
160-
let existingEmailUser;
161-
// Handle case where user has made multiple p5.js Editor accounts,
162-
// with emails that are connected to the same GitHub account
163-
if (existingEmailUsers.length > 1) {
164-
existingEmailUser = existingEmailUsers.find(
165-
(u) => (u.email = primaryEmail)
166-
);
167-
} else {
168-
[existingEmailUser] = existingEmailUsers;
169-
}
170-
if (existingEmailUser.banned) {
171-
done(null, false, { msg: accountSuspensionMessage });
172-
return;
173-
}
174-
existingEmailUser.email = existingEmailUser.email || primaryEmail;
175-
existingEmailUser.github = profile.id;
176-
existingEmailUser.username =
177-
existingEmailUser.username || profile.username;
178-
existingEmailUser.tokens.push({ kind: 'github', accessToken });
179-
existingEmailUser.name =
180-
existingEmailUser.name || profile.displayName;
181-
existingEmailUser.verified = User.EmailConfirmation.Verified;
182-
existingEmailUser.save((saveErr) =>
183-
done(null, existingEmailUser)
184-
);
185-
} else {
186-
let { username } = profile;
187-
User.findByUsername(
188-
username,
189-
{ caseInsensitive: true },
190-
(findByUsernameErr, existingUsernameUser) => {
191-
if (existingUsernameUser) {
192-
username = generateUniqueUsername(username);
193-
}
194-
const user = new User();
195-
user.email = primaryEmail;
196-
user.github = profile.id;
197-
user.username = profile.username;
198-
user.tokens.push({ kind: 'github', accessToken });
199-
user.name = profile.displayName;
200-
user.verified = User.EmailConfirmation.Verified;
201-
user.save((saveErr) => done(null, user));
202-
}
203-
);
204-
}
205-
});
155+
req.user.save();
156+
return done(null, req.user);
157+
}
158+
159+
const existingEmailUsers = await User.findAllByEmails(emails);
160+
161+
if (existingEmailUsers.length) {
162+
let existingEmailUser;
163+
164+
// Handle case where user has made multiple p5.js Editor accounts,
165+
// with emails that are connected to the same GitHub account
166+
if (existingEmailUsers.length > 1) {
167+
existingEmailUser = existingEmailUsers.find(
168+
(u) => (u.email = primaryEmail)
169+
);
170+
} else {
171+
[existingEmailUser] = existingEmailUsers;
172+
}
173+
174+
if (existingEmailUser.banned) {
175+
return done(null, false, { msg: accountSuspensionMessage });
176+
}
177+
existingEmailUser.email = existingEmailUser.email || primaryEmail;
178+
existingEmailUser.github = profile.id;
179+
existingEmailUser.username =
180+
existingEmailUser.username || profile.username;
181+
existingEmailUser.tokens.push({ kind: 'github', accessToken });
182+
existingEmailUser.name =
183+
existingEmailUser.name || profile.displayName;
184+
existingEmailUser.verified = User.EmailConfirmation.Verified;
185+
existingEmailUser.save();
186+
return done(null, existingEmailUser);
206187
}
207-
});
188+
189+
let { username } = profile;
190+
191+
const existingUsernameUser = await User.findByUsername(username, {
192+
caseInsensitive: true
193+
});
194+
195+
if (existingUsernameUser) {
196+
username = generateUniqueUsername(username);
197+
}
198+
const user = new User();
199+
user.email = primaryEmail;
200+
user.github = profile.id;
201+
user.username = profile.username;
202+
user.tokens.push({ kind: 'github', accessToken });
203+
user.name = profile.displayName;
204+
user.verified = User.EmailConfirmation.Verified;
205+
await user.save();
206+
207+
return done(null, user);
208+
} catch (err) {
209+
console.error(err);
210+
return done(null, false, { msg: err });
211+
}
208212
}
209213
)
210214
);
@@ -221,92 +225,78 @@ passport.use(
221225
passReqToCallback: true,
222226
scope: ['openid email']
223227
},
224-
(req, accessToken, refreshToken, profile, done) => {
225-
User.findOne(
226-
{ google: profile._json.emails[0].value },
227-
(findByGoogleErr, existingUser) => {
228-
if (existingUser) {
229-
if (req.user && req.user.email !== existingUser.email) {
230-
done(null, false, {
231-
msg: 'Google account is already linked to another account.'
232-
});
233-
return;
234-
} else if (existingUser.banned) {
235-
done(null, false, { msg: accountSuspensionMessage });
236-
return;
237-
}
238-
done(null, existingUser);
239-
return;
228+
async (req, accessToken, refreshToken, profile, done) => {
229+
try {
230+
const existingUser = await User.findOne({
231+
google: profile._json.emails[0].value
232+
});
233+
234+
if (existingUser) {
235+
if (req.user && req.user.email !== existingUser.email) {
236+
return done(null, false, {
237+
msg: 'Google account is already linked to another account.'
238+
});
239+
} else if (existingUser.banned) {
240+
return done(null, false, { msg: accountSuspensionMessage });
240241
}
241-
const primaryEmail = profile._json.emails[0].value;
242-
243-
if (req.user) {
244-
if (!req.user.google) {
245-
req.user.google = profile._json.emails[0].value;
246-
req.user.tokens.push({ kind: 'google', accessToken });
247-
req.user.verified = User.EmailConfirmation.Verified;
248-
}
249-
req.user.save((saveErr) => done(null, req.user));
250-
} else {
251-
User.findByEmail(
252-
primaryEmail,
253-
(findByEmailErr, existingEmailUser) => {
254-
let username = profile._json.emails[0].value.split('@')[0];
255-
User.findByUsername(
256-
username,
257-
{ caseInsensitive: true },
258-
(findByUsernameErr, existingUsernameUser) => {
259-
if (existingUsernameUser) {
260-
username = generateUniqueUsername(username);
261-
}
262-
// what if a username is already taken from the display name too?
263-
// then, append a random friendly word?
264-
if (existingEmailUser) {
265-
if (existingEmailUser.banned) {
266-
done(null, false, { msg: accountSuspensionMessage });
267-
return;
268-
}
269-
existingEmailUser.email =
270-
existingEmailUser.email || primaryEmail;
271-
existingEmailUser.google = profile._json.emails[0].value;
272-
existingEmailUser.username =
273-
existingEmailUser.username || username;
274-
existingEmailUser.tokens.push({
275-
kind: 'google',
276-
accessToken
277-
});
278-
existingEmailUser.name =
279-
existingEmailUser.name || profile._json.displayName;
280-
existingEmailUser.verified =
281-
User.EmailConfirmation.Verified;
282-
existingEmailUser.save((saveErr) => {
283-
if (saveErr) {
284-
console.log(saveErr);
285-
}
286-
done(null, existingEmailUser);
287-
});
288-
} else {
289-
const user = new User();
290-
user.email = primaryEmail;
291-
user.google = profile._json.emails[0].value;
292-
user.username = username;
293-
user.tokens.push({ kind: 'google', accessToken });
294-
user.name = profile._json.displayName;
295-
user.verified = User.EmailConfirmation.Verified;
296-
user.save((saveErr) => {
297-
if (saveErr) {
298-
console.log(saveErr);
299-
}
300-
done(null, user);
301-
});
302-
}
303-
}
304-
);
305-
}
306-
);
242+
return done(null, existingUser);
243+
}
244+
245+
const primaryEmail = profile._json.emails[0].value;
246+
247+
if (req.user) {
248+
if (!req.user.google) {
249+
req.user.google = profile._json.emails[0].value;
250+
req.user.tokens.push({ kind: 'google', accessToken });
251+
req.user.verified = User.EmailConfirmation.Verified;
307252
}
253+
req.user.save();
254+
return done(null, req.user);
255+
}
256+
let username = profile._json.emails[0].value.split('@')[0];
257+
const existingEmailUser = await User.findByEmail(primaryEmail);
258+
const existingUsernameUser = await User.findByUsername(username, {
259+
caseInsensitive: true
260+
});
261+
262+
if (existingUsernameUser) {
263+
username = generateUniqueUsername(username);
264+
}
265+
// what if a username is already taken from the display name too?
266+
// then, append a random friendly word?
267+
if (existingEmailUser) {
268+
if (existingEmailUser.banned) {
269+
return done(null, false, { msg: accountSuspensionMessage });
270+
}
271+
existingEmailUser.email = existingEmailUser.email || primaryEmail;
272+
existingEmailUser.google = profile._json.emails[0].value;
273+
existingEmailUser.username = existingEmailUser.username || username;
274+
existingEmailUser.tokens.push({
275+
kind: 'google',
276+
accessToken
277+
});
278+
existingEmailUser.name =
279+
existingEmailUser.name || profile._json.displayName;
280+
existingEmailUser.verified = User.EmailConfirmation.Verified;
281+
282+
await existingEmailUser.save();
283+
return done(null, existingEmailUser);
308284
}
309-
);
285+
286+
const user = new User();
287+
user.email = primaryEmail;
288+
user.google = profile._json.emails[0].value;
289+
user.username = username;
290+
user.tokens.push({ kind: 'google', accessToken });
291+
user.name = profile._json.displayName;
292+
user.verified = User.EmailConfirmation.Verified;
293+
294+
await user.save();
295+
return done(null, user);
296+
} catch (err) {
297+
console.error(err);
298+
return done(null, false, { msg: err });
299+
}
310300
}
311301
)
312302
);

0 commit comments

Comments
 (0)