Lin / ACCOUNT_CREATION_FIX_PLAN.md
Zelyanoth's picture
kk
e4de23f

Account Creation Fix Implementation Plan

Executive Summary

The account creation process is failing during the OAuth callback phase. While the initial OAuth flow and callback are successful, the database insertion step is not working properly, resulting in accounts not being saved to the database.

Root Cause Analysis

Based on the log analysis, the issue is in the OAuth callback handler in backend/api/accounts.py. The handler receives the OAuth code and state but fails to properly insert the account data into the Supabase Social_network table.

Implementation Plan

Phase 1: Immediate Debugging & Logging (Priority: High)

1.1 Enhanced Logging for OAuth Callback

File: backend/api/accounts.py Changes:

  • Add detailed logging at the beginning of handle_oauth_callback
  • Log all received parameters (user_id, code, state, social_network)
  • Add logging before and after database insertion
  • Log the exact data being inserted
# Add to handle_oauth_callback function
current_app.logger.info(f"πŸ”— [OAuth] Starting callback for user: {user_id}")
current_app.logger.info(f"πŸ”— [OAuth] Received data: {data}")
current_app.logger.info(f"πŸ”— [OAuth] Supabase client available: {hasattr(current_app, 'supabase')}")

1.2 Database Connection Verification

File: backend/api/accounts.py Changes:

  • Add verification that Supabase client is properly initialized
  • Log the exact table name and data structure being used
  • Add error handling for database connection issues

1.3 Response Validation

File: backend/api/accounts.py Changes:

  • Add detailed logging of the database response
  • Log whether response.data exists and what it contains
  • Add validation to ensure the insertion was successful

Phase 2: Database Schema & Permissions (Priority: High)

2.1 Table Structure Verification

Action: Verify the Social_network table structure in Supabase Required Fields:

  • id (UUID, primary key)
  • social_network (text)
  • account_name (text)
  • id_utilisateur (text, foreign key to users)
  • token (text, nullable)
  • sub (text, nullable)
  • given_name (text, nullable)
  • family_name (text, nullable)
  • picture (text, nullable)

2.2 RLS (Row Level Security) Policies

Action: Check RLS policies on the Social_network table Expected Policy: Users should only be able to insert/update their own records Verification: Ensure the policy allows id_utilisateur = auth.uid()

2.3 Database Permissions

Action: Verify service account has proper permissions Required Permissions: INSERT, SELECT, UPDATE, DELETE on Social_network table

Phase 3: OAuth Flow Verification (Priority: Medium)

3.1 Frontend Callback Handling

File: frontend/src/components/LinkedInAccount/LinkedInCallbackHandler.jsx Changes:

  • Add logging to verify the callback is being processed
  • Log the URL parameters (code, state, error)
  • Verify the correct API endpoint is being called

3.2 API Endpoint Verification

File: frontend/src/services/accountService.js Changes:

  • Add logging to verify the callback request is being sent
  • Log the request payload and response
  • Verify the correct endpoint /accounts/callback is being used

Phase 4: Data Validation & Error Handling (Priority: Medium)

4.1 Input Validation

File: backend/api/accounts.py Changes:

  • Add comprehensive validation for all required fields
  • Add logging for validation failures
  • Provide specific error messages for missing fields

4.2 Error Handling Enhancement

File: backend/api/accounts.py Changes:

  • Add specific error logging for different failure scenarios
  • Include the original error message in the response
  • Add try-catch blocks around database operations

Phase 5: Testing & Verification (Priority: Medium)

5.1 Unit Testing

Action: Create unit tests for the OAuth callback handler Coverage:

  • Successful OAuth flow
  • Missing required fields
  • Database connection failures
  • Invalid user ID

5.2 Integration Testing

Action: Test the complete OAuth flow end-to-end Steps:

  1. Initiate OAuth flow
  2. Complete LinkedIn authentication
  3. Handle callback in frontend
  4. Process callback in backend
  5. Verify database insertion
  6. Confirm account appears in UI

5.3 Database Testing

Action: Test database operations directly Verification:

  • Manual insertion of test data
  • Verification of RLS policies
  • Testing of error scenarios

Implementation Timeline

Week 1: Immediate Fixes

  • Add enhanced logging to OAuth callback handler
  • Verify database connection and permissions
  • Test basic database insertion
  • Fix any immediate issues found

Week 2: Database & Security

  • Verify table structure and constraints
  • Check RLS policies and permissions
  • Implement proper error handling
  • Add input validation

Week 3: Testing & Verification

  • Create comprehensive unit tests
  • Perform end-to-end integration testing
  • Test error scenarios and edge cases
  • Verify fix works in production environment

Success Criteria

  1. βœ… OAuth callback completes successfully
  2. βœ… Account data is properly inserted into database
  3. βœ… Account appears in user account list
  4. βœ… Error handling provides clear feedback
  5. βœ… All tests pass

Monitoring & Maintenance

  1. Logging: Monitor OAuth callback success/failure rates
  2. Database: Regular backup and maintenance
  3. Security: Regular review of RLS policies and permissions
  4. Performance: Monitor database query performance

Risk Assessment

  • High Risk: Database connection issues
  • Medium Risk: RLS policy conflicts
  • Low Risk: Frontend callback handling

Contingency Plans

  1. Database Issues: Implement fallback logging and manual account creation
  2. OAuth Issues: Provide alternative authentication methods
  3. Performance Issues: Implement database connection pooling