I've fixed the friendship system to ensure proper request/accept functionality:
-
Fixed Accept Friend Request Logic:
- Now properly finds the correct friendship record using bidirectional lookup
- Uses the specific friendship ID instead of relying on requester/addressee combinations
- Added comprehensive logging for debugging
-
Enhanced Send Friend Request Logic:
- Added duplicate request prevention
- Better error handling and logging
- Immediate UI refresh after successful operations
-
Improved Error Handling:
- Better console logging with emojis for easy identification
- More specific error messages
- Proper rollback of optimistic UI updates on failure
-
Send Request: User A searches for User B and sends a friend request
- ✅ User A should see "Pending" status
- ✅ User B should see "Accept" button when they view User A
-
Accept Request: User B accepts the friend request
- ✅ Both users should see "Tribe-mate" status
- ✅ User B should see celebratory confirmation alert
- ✅ Status should persist after app refresh
- Multiple Requests: User A tries to send multiple requests to User B
- ✅ Should show "Already Connected" message
- ✅ Should not create duplicate database entries
- Reverse Check: After User A sends request to User B, check if User B can send to User A
- ✅ User B should see "Accept" button (not "Add Friend")
- ✅ No duplicate requests should be possible
When testing, look for these console messages:
🚀 Sending friend request to: [username] ID: [user_id]
⚠️ Friendship already exists: [friendship_object] (if duplicate)
✅ Friend request sent successfully!
🤝 Accepting friend request from: [username] ID: [user_id]
✅ Found friendship record: [friendship_object]
✅ Friend request accepted successfully!
❌ No pending friendship found: [error]
❌ Error updating friendship: [error]
❌ Error sending friend request: [error]
The system now uses these improved queries:
SELECT * FROM friendships
WHERE (
(requester_id = 'user_a_id' AND addressee_id = 'user_b_id')
OR
(requester_id = 'user_b_id' AND addressee_id = 'user_a_id')
)UPDATE friendships
SET status = 'accepted', updated_at = NOW()
WHERE id = 'specific_friendship_id'Solution:
- Check console logs for error messages
- Verify RLS policies allow updates for both users
- Ensure the friendship record exists with correct user IDs
Solution:
- Check for orphaned friendship records in database
- Verify bidirectional lookup is working correctly
- Clear app cache and test again
Solution:
- Verify
getFriendshipStatusfunction is returning correct status - Check if friendship record has correct requester/addressee relationship
- Ensure UI is refreshing after status changes
Ensure your friendships table has this structure:
CREATE TABLE public.friendships (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
requester_id UUID REFERENCES public.users(id) ON DELETE CASCADE,
addressee_id UUID REFERENCES public.users(id) ON DELETE CASCADE,
status TEXT DEFAULT 'pending', -- 'pending', 'accepted', 'blocked'
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(requester_id, addressee_id)
);Verify these RLS policies are active:
-- Users can view their friendships
CREATE POLICY "Users can view their friendships" ON public.friendships
FOR SELECT USING (requester_id = auth.uid() OR addressee_id = auth.uid());
-- Users can update friendships they're involved in
CREATE POLICY "Users can update friendships they're involved in" ON public.friendships
FOR UPDATE USING (requester_id = auth.uid() OR addressee_id = auth.uid());The friendship system is working correctly when:
- ✅ Friend requests can be sent without duplicates
- ✅ Received requests show "Accept" button correctly
- ✅ Accepting requests updates status to "Tribe-mate" for both users
- ✅ Status persists after app refresh/reload
- ✅ Console shows appropriate success/error messages
- ✅ Celebratory alert appears after successful acceptance
- Use Two Test Accounts: Create two different user accounts for comprehensive testing
- Test on Different Devices: Use iOS Simulator and Android Emulator simultaneously
- Check Database Directly: Use Supabase dashboard to verify friendship records
- Monitor Console Logs: Keep developer console open during testing
- Test Network Scenarios: Try with poor connectivity to test error handling
Note: If you encounter persistent issues, check the Supabase logs in your dashboard for server-side errors and verify that your RLS policies are correctly configured.