fix: prevent admin status reset on login (#659)

This commit is contained in:
Thomas Durieux
2026-04-15 04:34:36 +02:00
committed by GitHub
parent f3641c8ce3
commit c6d6806d7a
+34 -23
View File
@@ -39,21 +39,31 @@ const verify = async (
{ "source.accessToken": accessToken } { "source.accessToken": accessToken }
); );
} else { } else {
const photo = profile.photos ? profile.photos[0]?.value : null; // Check if a user with this username already exists (e.g. created
user = new UserModel({ // manually without externalIDs.github). Link the GitHub ID to the
username: profile.username, // existing account instead of creating a duplicate that would lose
accessTokens: { // the isAdmin flag.
github: accessToken, user = await UserModel.findOne({ username: profile.username });
}, if (user) {
externalIDs: { user.externalIDs.github = profile.id;
github: profile.id, user.accessTokens.github = accessToken;
}, } else {
emails: profile.emails?.map((email) => { const photo = profile.photos ? profile.photos[0]?.value : null;
return { email: email.value, default: false }; user = new UserModel({
}), username: profile.username,
photo, accessTokens: {
}); github: accessToken,
if (user.emails?.length) user.emails[0].default = true; },
externalIDs: {
github: profile.id,
},
emails: profile.emails?.map((email) => {
return { email: email.value, default: false };
}),
photo,
});
if (user.emails?.length) user.emails[0].default = true;
}
} }
if (!user.accessTokenDates) { if (!user.accessTokenDates) {
user.accessTokenDates = { user.accessTokenDates = {
@@ -63,14 +73,6 @@ const verify = async (
user.accessTokenDates.github = new Date(); user.accessTokenDates.github = new Date();
} }
await user.save(); await user.save();
} catch (error) {
console.error(error);
throw new AnonymousError("unable_to_connect_user", {
httpStatus: 500,
object: profile,
cause: error as Error,
});
} finally {
done(null, { done(null, {
username: profile.username, username: profile.username,
accessToken, accessToken,
@@ -78,6 +80,15 @@ const verify = async (
profile, profile,
user, user,
}); });
} catch (error) {
console.error(error);
done(
new AnonymousError("unable_to_connect_user", {
httpStatus: 500,
object: profile,
cause: error as Error,
})
);
} }
}; };