Skip to content

Latest commit

 

History

History
123 lines (93 loc) · 4.66 KB

File metadata and controls

123 lines (93 loc) · 4.66 KB

"User not found" Error Fix - Recruiter Profile Creation

Problem

When trying to create a recruiter profile, users were getting a "User not found" error, even though they were properly authenticated.

Root Cause Analysis

The issue was in the RecruiterProfileService.createProfile method in src/services/recruiter-profile.ts. The service was performing an explicit check to verify if the user exists in the database before creating the recruiter profile:

// Check if user exists
const existingUser = await db
  .select()
  .from(user)
  .where(eq(user.id, userId))
  .limit(1);

if (existingUser.length === 0) {
  throw new Error('User not found');
}

This check was failing because:

  1. Timing Issue: The user might be authenticated via Better Auth but not yet fully persisted to the database
  2. Session vs Database Mismatch: The user ID from the session might not immediately match what's in the database
  3. Unnecessary Validation: Since Better Auth handles user management, this explicit check was redundant

Solution

1. Removed Redundant User Existence Check

File: src/services/recruiter-profile.ts

Replaced the explicit user existence check with a trust-based approach:

// Note: We trust that Better Auth has already validated the user exists
// since they have a valid session. The foreign key constraint on recruiterProfiles
// will catch any issues if the user doesn't exist.
console.log('[RECRUITER-PROFILE-SERVICE] Proceeding with profile creation for user:', userId);

2. Enhanced Error Handling for Database Constraints

File: src/services/recruiter-profile.ts

Added proper error handling for foreign key constraint violations:

try {
  await db.insert(recruiterProfiles).values(newProfile);
  console.log('[RECRUITER-PROFILE-SERVICE] Profile created successfully');
} catch (dbError) {
  console.error('[RECRUITER-PROFILE-SERVICE] Database insert failed:', dbError);
  
  // Check if it's a foreign key constraint error (user doesn't exist)
  if (dbError instanceof Error && dbError.message.includes('foreign key')) {
    console.log('[RECRUITER-PROFILE-SERVICE] Foreign key constraint failed - user does not exist');
    throw new Error('User not found in database. Please ensure you are properly authenticated.');
  }
  
  // Re-throw other database errors
  throw dbError;
}

3. Added Debug Endpoint for Troubleshooting

File: src/app/api/debug/user-session/route.ts

Created a debug endpoint to help diagnose authentication and user database issues:

  • Checks session data
  • Verifies user existence in database
  • Lists all users for comparison
  • Provides detailed logging

4. Enhanced Profile Page with Debug Tools

File: src/app/recruiter/profile/page.tsx

Added debug functionality (development only) to help troubleshoot user session issues.

Why This Fix Works

  1. Trust Better Auth: Better Auth is responsible for user management, so if a user has a valid session, we can trust they exist
  2. Database Constraints: The foreign key constraint on recruiterProfiles.userId will catch any actual user existence issues
  3. Better Error Messages: If there is a real user issue, the foreign key error provides a clearer message
  4. Reduced Race Conditions: Eliminates timing issues between session creation and database persistence

Testing the Fix

Before Fix:

  1. User logs in successfully
  2. Tries to create recruiter profile
  3. Gets "User not found" error
  4. Profile creation fails

After Fix:

  1. User logs in successfully
  2. Tries to create recruiter profile
  3. Profile creation succeeds (or gives specific database error if there's a real issue)
  4. User can proceed with recruiter functionality

Debug Testing:

  1. Visit /recruiter/profile in development mode
  2. Click "Debug User Session" button
  3. Check console and alert for user session information
  4. Verify user exists in database

Files Modified

  1. src/services/recruiter-profile.ts - Removed redundant user check, enhanced error handling
  2. src/app/api/debug/user-session/route.ts - New debug endpoint
  3. src/app/recruiter/profile/page.tsx - Added debug functionality

Security Considerations

  • The fix maintains security by relying on Better Auth's authentication
  • Foreign key constraints still prevent invalid user references
  • Debug endpoints are only available in development mode
  • Error messages don't expose sensitive system information

Future Improvements

  1. Consider adding user synchronization checks if timing issues persist
  2. Implement retry logic for profile creation if needed
  3. Add more comprehensive user validation in the auth callbacks
  4. Monitor for foreign key constraint violations to catch edge cases