mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:51:13 -05:00
feat: Update Discord username in edge function
This commit is contained in:
@@ -43,6 +43,35 @@ interface DiscordUserMetadata {
|
||||
iss?: string;
|
||||
}
|
||||
|
||||
async function ensureUniqueUsername(
|
||||
supabase: any,
|
||||
baseUsername: string,
|
||||
userId: string,
|
||||
maxAttempts: number = 10
|
||||
): Promise<string> {
|
||||
let username = baseUsername.toLowerCase();
|
||||
let attempt = 0;
|
||||
|
||||
while (attempt < maxAttempts) {
|
||||
const { data: existing } = await supabase
|
||||
.from('profiles')
|
||||
.select('user_id')
|
||||
.eq('username', username)
|
||||
.neq('user_id', userId)
|
||||
.maybeSingle();
|
||||
|
||||
if (!existing) {
|
||||
return username;
|
||||
}
|
||||
|
||||
attempt++;
|
||||
username = `${baseUsername.toLowerCase()}_${attempt}`;
|
||||
}
|
||||
|
||||
// Fallback to UUID-based username
|
||||
return `user_${userId.substring(0, 8)}`;
|
||||
}
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
if (req.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
@@ -275,6 +304,13 @@ Deno.serve(async (req) => {
|
||||
updateData.display_name = displayName;
|
||||
}
|
||||
|
||||
// Update username if it's currently a generic UUID-based username
|
||||
if (usernameBase && profile?.username?.startsWith('user_')) {
|
||||
const newUsername = await ensureUniqueUsername(supabase, usernameBase, user.id);
|
||||
updateData.username = newUsername;
|
||||
console.log('[OAuth Profile] Updating generic username from', profile.username, 'to', newUsername);
|
||||
}
|
||||
|
||||
// Only update if we have data to update
|
||||
if (Object.keys(updateData).length > 0) {
|
||||
const { error: updateError } = await supabase
|
||||
|
||||
Reference in New Issue
Block a user