From eece956c166a35a933129046cf4a3dd6c658e1e5 Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Sat, 31 Oct 2026 01:42:41 +0100 Subject: [PATCH] Use the correct default avatar for users without a discriminator Co-Authored-By: Claude Opus 5.5 --- src/app/lib/auth.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/app/lib/auth.ts b/src/app/lib/auth.ts index 31b14c0..1d81233 100644 --- a/src/app/lib/auth.ts +++ b/src/app/lib/auth.ts @@ -12,6 +12,18 @@ async function checkRole(userId: string): Promise { return hasRole; } +function getAvatarUrl(profile: DiscordProfile): string { + if (profile.avatar) { + const format = profile.avatar.startsWith("a_") ? "gif" : "png"; + return `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`; + } + const index = + profile.discriminator === "0" + ? Number((BigInt(profile.id) >> BigInt(22)) % BigInt(6)) + : Number(profile.discriminator) % 5; + return `https://cdn.discordapp.com/embed/avatars/${index}.png`; +} + export const authOptions: NextAuthOptions = { providers: [ DiscordProvider({ @@ -19,18 +31,11 @@ export const authOptions: NextAuthOptions = { clientSecret: process.env.DISCORD_CLIENT_SECRET as string, authorization: { params: { scope: "identify" } }, profile(profile: DiscordProfile) { - if (profile.avatar === null) { - const defaultAvatarNumber = parseInt(profile.discriminator) % 5; - profile.image_url = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarNumber}.png`; - } else { - const format = profile.avatar.startsWith("a_") ? "gif" : "png"; - profile.image_url = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`; - } return { id: profile.id, name: profile.global_name ?? profile.username, email: null, - image: profile.image_url, + image: getAvatarUrl(profile), }; }, }),