diff --git a/supabase/functions/process-oauth-profile/index.ts b/supabase/functions/process-oauth-profile/index.ts index b87c0fba..e2c7947b 100644 --- a/supabase/functions/process-oauth-profile/index.ts +++ b/supabase/functions/process-oauth-profile/index.ts @@ -31,8 +31,12 @@ interface DiscordUserMetadata { email?: string; username?: string; global_name?: string; + discriminator?: string; avatar?: string; id?: string; + verified?: boolean; + flags?: number; + premium_type?: number; } Deno.serve(async (req) => { @@ -75,12 +79,21 @@ Deno.serve(async (req) => { const discordIdentity = user.identities.find(i => i.provider === 'discord'); if (discordIdentity) { userMetadata = discordIdentity.identity_data || {}; + + // Debug: Log full identity_data to see what Discord actually returns + console.log('[OAuth Profile] Discord identity_data (full):', JSON.stringify(discordIdentity.identity_data)); + console.log('[OAuth Profile] Using Discord identity data:', { hasAvatar: !!userMetadata.avatar, hasUsername: !!userMetadata.username, hasGlobalName: !!userMetadata.global_name, - hasId: !!userMetadata.id + hasId: !!userMetadata.id, + hasEmail: !!userMetadata.email, + avatarValue: userMetadata.avatar, + idValue: userMetadata.id }); + } else { + console.warn('[OAuth Profile] Discord provider found but no Discord identity in user.identities'); } } @@ -97,18 +110,35 @@ Deno.serve(async (req) => { console.log('[OAuth Profile] Google user:', { avatarUrl, displayName, usernameBase }); } else if (provider === 'discord') { const discordData = userMetadata as DiscordUserMetadata; + + // Prefer global_name, fall back to username displayName = discordData.global_name || discordData.username || null; usernameBase = discordData.username || null; - // Extract email (Discord provides it) + // Extract email (Discord provides it with email scope) const discordEmail = discordData.email || null; + // Validate we have minimum required data + if (!discordData.id) { + console.error('[OAuth Profile] Discord user ID missing - OAuth scopes may not be configured correctly'); + } + + if (!discordData.username) { + console.warn('[OAuth Profile] Discord username missing - using ID as fallback'); + usernameBase = discordData.id || null; + } + // Construct Discord avatar URL with proper format detection if (discordData.avatar && discordData.id) { // Discord animated avatars have 'a_' prefix and use .gif extension const isAnimated = discordData.avatar.startsWith('a_'); const extension = isAnimated ? 'gif' : 'png'; avatarUrl = `https://cdn.discordapp.com/avatars/${discordData.id}/${discordData.avatar}.${extension}?size=512`; + } else if (!discordData.avatar && discordData.id) { + // User has no custom avatar - use Discord default avatar + const defaultAvatarIndex = (parseInt(discordData.id) >> 22) % 6; + avatarUrl = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarIndex}.png`; + console.log('[OAuth Profile] Using Discord default avatar for user without custom avatar'); } console.log('[OAuth Profile] Discord user:', { @@ -116,7 +146,8 @@ Deno.serve(async (req) => { displayName, usernameBase, email: discordEmail, - hasAnimatedAvatar: discordData.avatar?.startsWith('a_') + hasAnimatedAvatar: discordData.avatar?.startsWith('a_'), + hasCustomAvatar: !!discordData.avatar }); } else { console.log('[OAuth Profile] Unsupported provider:', provider);