Files
orangecat/scripts/apply-function-fix.js
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

80 lines
2.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Apply handle_new_user function fix
*
* Fixes the database function to use email prefix instead of full email
*/
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);
const sql = `
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, username, name, created_at, updated_at)
VALUES (
NEW.id,
-- Use email prefix (before @) instead of full email
COALESCE(
SPLIT_PART(NEW.email, '@', 1),
'user_' || SUBSTRING(NEW.id::TEXT, 1, 8)
),
COALESCE(
NEW.raw_user_meta_data->>'full_name',
NEW.raw_user_meta_data->>'name',
SPLIT_PART(NEW.email, '@', 1),
'User'
),
NOW(),
NOW()
)
ON CONFLICT (id) DO NOTHING;
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
`;
async function applyFix() {
console.log('Applying handle_new_user function fix...\n');
const { error } = await supabase.rpc('exec_sql', { sql });
if (error) {
console.error('Error applying fix:', error);
// Try alternative approach using a direct query
console.log('\nTrying direct query approach...');
const { error: directError } = await supabase
.from('_realtime_schema_changes')
.select('*')
.limit(0);
if (directError) {
console.error('Cannot apply SQL directly. Please apply manually using Supabase dashboard.');
console.log('\nSQL to apply:');
console.log(sql);
process.exit(1);
}
} else {
console.log('✓ Function fix applied successfully!');
}
}
applyFix().catch(error => {
console.error('Unexpected error:', error);
console.log('\nPlease apply this SQL manually using Supabase SQL Editor:');
console.log(sql);
process.exit(1);
});