Files
orangecat/scripts/fix-username-emails.js
T
Your NameandClaude 61a94449ea fix(profiles): Fix username field containing email addresses instead of usernames
## Problem
- ProfilePeopleTab showed "Profile Not Found" when clicking on followed users
- Server logs showed requests to `/profiles/adelina1996gry@gmail.com`
- Root cause: `handle_new_user()` function set username to full email instead of email prefix

## Solution
1. Created migration to fix `handle_new_user()` function:
   - Changed from `COALESCE(NEW.email, ...)` to `COALESCE(SPLIT_PART(NEW.email, '@', 1), ...)`
   - This extracts username from email (part before @)

2. Created and ran script to fix existing profiles:
   - Fixed profile: adelina1996gry@gmail.com → adelina1996gry
   - Ensures uniqueness by adding numeric suffix if needed

## Files
- supabase/migrations/20251119_fix_username_email_issue.sql - Migration for function fix
- scripts/fix-username-emails.js - Script to update existing profiles
- scripts/fix-handle-new-user-function.sql - Manual SQL if needed
- scripts/apply-function-fix.js - Attempted API approach

## Testing
✓ Profile links now work: `/profiles/adelina1996gry` instead of 404
✓ No more `/profiles/undefined` requests
✓ ProfilePeopleTab displays correctly

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-19 16:46:57 +01:00

85 lines
2.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Fix Username Email Issue
*
* This script fixes profiles that have full email addresses as usernames
* by extracting the part before @ and ensuring uniqueness.
*/
const { createClient } = require('@supabase/supabase-js');
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
if (!supabaseUrl || !supabaseServiceKey) {
console.error('Missing SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY');
process.exit(1);
}
const supabase = createClient(supabaseUrl, supabaseServiceKey);
async function fixUsernameEmails() {
console.log('Starting username fix...\n');
// Step 1: Get all profiles with email addresses as usernames
const { data: profiles, error: fetchError } = await supabase
.from('profiles')
.select('id, username, email')
.like('username', '%@%');
if (fetchError) {
console.error('Error fetching profiles:', fetchError);
process.exit(1);
}
console.log(`Found ${profiles.length} profiles with email addresses as usernames\n`);
// Step 2: Fix each profile
for (const profile of profiles) {
const baseUsername = profile.username.split('@')[0];
let newUsername = baseUsername;
let counter = 0;
// Check for uniqueness
let isUnique = false;
while (!isUnique) {
const { data: existing } = await supabase
.from('profiles')
.select('id')
.eq('username', newUsername)
.neq('id', profile.id)
.single();
if (!existing) {
isUnique = true;
} else {
counter++;
newUsername = `${baseUsername}${counter}`;
}
}
// Update the profile
const { error: updateError } = await supabase
.from('profiles')
.update({
username: newUsername,
updated_at: new Date().toISOString(),
})
.eq('id', profile.id);
if (updateError) {
console.error(`Error updating profile ${profile.id}:`, updateError);
} else {
console.log(`✓ Updated profile ${profile.id}: ${profile.username} → ${newUsername}`);
}
}
console.log('\n✓ Username fix completed!');
}
fixUsernameEmails().catch(error => {
console.error('Unexpected error:', error);
process.exit(1);
});