Files
orangecat/scripts/fix-handle-new-user-function.sql
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

26 lines
689 B
PL/PgSQL

-- Fix handle_new_user function to use email prefix instead of full email address
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;