Skip to content

feat: Auto-provision users during OAuth login #379

@nycomp

Description

@nycomp

Background

When users sign in to campus-admin via OAuth (Google, GitHub, etc.), the authentication flow completes but the application crashes with a 500 error:

ServerError: Document/row with id 'test_student@nyjc.edu.sg' not found in collection/table 'users'

Problem Investigation

The OAuth callback flow works like this:

  1. User clicks "Sign In" → redirected to Google/GitHub/etc.
  2. Provider redirects back with authorization code
  3. Backend exchanges code for access token and user info (email, name)
  4. Credentials are stored in the database
  5. Session is updated with the user_id
  6. User is redirected back to campus-admin

However, when campus-admin tries to display the dashboard, it calls push_context() to load the authenticated user's details. This looks up the user in the users table... but the user record was never created in step 4.

Your Task

Implement auto-provisioning of user records during the OAuth callback flow. When a user successfully authenticates via OAuth, their user record should be created automatically if it doesn't already exist.

Key Requirements

  1. Idempotent operation: If the user already exists, return the existing record
  2. Minimal database changes: Only create if necessary
  3. Consistent across providers: All OAuth providers (Google, GitHub, Discord) should use the same mechanism
  4. Thread-safe: Handle race conditions if two requests try to create the same user simultaneously

Files to Investigate

  • campus/auth/resources/user.py - User resource with CRUD operations
  • campus/auth/oauth_proxy/google/proxy.py - Google OAuth callback
  • campus/auth/oauth_proxy/github/proxy.py - GitHub OAuth callback
  • campus/auth/oauth_proxy/discord/proxy.py - Discord OAuth callback

Acceptance Criteria

  • User records are created automatically on first OAuth login
  • Subsequent logins retrieve the existing user record
  • No 500 errors when new users log in
  • All three OAuth providers (Google, GitHub, Discord) work correctly

Testing Hints

  • Check the Railway logs for campus-auth after deploying your changes
  • Try logging in with a test account that doesn't exist in the database
  • Verify the user record is created correctly with email and name

Subscribed: @saltensity

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

Status

Testing/Integration

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions