Fix GRO-1118: Auto-create staff records for OAuth users #4

Closed
groombook-engineer[bot] wants to merge 1 commits from flea-flicker/auto-create-staff-oauth-users-v2 into dev
groombook-engineer[bot] commented 2026-05-12 19:19:13 +00:00 (Migrated from github.com)

Summary

Fixes GRO-1118 - uat-tester receives HTTP 403 on all app routes post-login

Problem

When a user authenticates via OAuth (Authentik SSO), Better-Auth creates a user record in the `user` table, but there's no corresponding staff record. The RBAC middleware returns 403 Forbidden when no staff record exists for the authenticated user.

This happened to uat-tester after the GitRepository fix (PR #335) resolved the `email_is_missing` OAuth error. The user could authenticate successfully but couldn't access any routes.

Solution

Modified the RBAC middleware to auto-create staff records for authenticated OAuth users when no corresponding staff record exists. The middleware now checks for staff records in this order:

  1. By `userId` (Better-Auth user ID)
  2. By `oidcSub` (legacy OIDC subject for older records)
  3. By email (auto-link existing staff record with no userId)
  4. NEW: Create new staff record if authenticated user has email and name

New staff records are created with a default `receptionist` role and `active: true`. This allows new OAuth users to access the app immediately.

Changes

  • `apps/api/src/middleware/rbac.ts`: Added auto-create staff record logic
    • Removed duplicate `StaffRole` type (now inferred from `staffRoleEnum`)
    • Added staff record creation for authenticated users with email and name
    • Properly handles the case where insert might return undefined

Testing

  • All existing RBAC tests pass
  • Type checking passes with no errors

Impact

  • New OAuth users can now access the app without manual staff record creation
  • Existing auto-link by email functionality continues to work
  • No breaking changes to existing behavior

cc @cpfarhood

## Summary Fixes GRO-1118 - uat-tester receives HTTP 403 on all app routes post-login ## Problem When a user authenticates via OAuth (Authentik SSO), Better-Auth creates a user record in the \`user\` table, but there's no corresponding staff record. The RBAC middleware returns 403 Forbidden when no staff record exists for the authenticated user. This happened to uat-tester after the GitRepository fix (PR #335) resolved the \`email_is_missing\` OAuth error. The user could authenticate successfully but couldn't access any routes. ## Solution Modified the RBAC middleware to auto-create staff records for authenticated OAuth users when no corresponding staff record exists. The middleware now checks for staff records in this order: 1. By \`userId\` (Better-Auth user ID) 2. By \`oidcSub\` (legacy OIDC subject for older records) 3. By email (auto-link existing staff record with no userId) 4. **NEW**: Create new staff record if authenticated user has email and name New staff records are created with a default \`receptionist\` role and \`active: true\`. This allows new OAuth users to access the app immediately. ## Changes - \`apps/api/src/middleware/rbac.ts\`: Added auto-create staff record logic - Removed duplicate \`StaffRole\` type (now inferred from \`staffRoleEnum\`) - Added staff record creation for authenticated users with email and name - Properly handles the case where insert might return undefined ## Testing - All existing RBAC tests pass - Type checking passes with no errors ## Impact - New OAuth users can now access the app without manual staff record creation - Existing auto-link by email functionality continues to work - No breaking changes to existing behavior cc @cpfarhood
Owner

Closing as stale and superseded. This PR (GRO-1118: auto-create staff for OAuth) is not mergeable due to conflicts, has no reviews, and the underlying problem (HTTP 403 on authenticated routes post-login) has been addressed by the more complete implementation in PR #19 (GRO-1272: auto-provision staff on OIDC login). Closing per org-wide PR backlog cleanup (GRO-1355).

Closing as stale and superseded. This PR (GRO-1118: auto-create staff for OAuth) is not mergeable due to conflicts, has no reviews, and the underlying problem (HTTP 403 on authenticated routes post-login) has been addressed by the more complete implementation in PR #19 (GRO-1272: auto-provision staff on OIDC login). Closing per org-wide PR backlog cleanup ([GRO-1355](/GRO/issues/GRO-1355)).
Scrubs McBarkley closed this pull request 2026-05-20 12:10:46 +00:00

Pull request closed

Sign in to join this conversation.