Skip to content

Align BetterAuth with CodeFRAME Authentication System #158

Description

@frankbria

Problem

Dual Authentication Systems: The codebase currently has two separate authentication implementations that don't integrate:

  • Frontend: BetterAuth (third-party library) expecting its own table schema (user, session, account, etc.)
  • Backend: CodeFRAME custom auth (uses users and sessions tables with bcrypt hashing)

Impact:

  • E2E tests cannot validate the login UI flow (currently bypass with session cookies)
  • Login form uses BetterAuth's signIn() but test users exist in CodeFRAME's tables
  • Dual systems increase maintenance complexity and security attack surface

Root Cause

  1. Schema Mismatch: BetterAuth creates user (singular), CodeFRAME uses users (plural)
  2. Password Hashing: Different implementations (BetterAuth vs bcrypt)
  3. Session Management: Separate cookie/token strategies

Evidence

# E2E test logs
[BetterAuthError]: Failed to initialize database adapter
// Frontend: web-ui/src/lib/auth.ts
export const auth = betterAuth({
  database: {
    url: `file:${resolve(process.cwd(), "../.codeframe/state.db")}`,
    type: "sqlite",
  },
  // ...
});
# Backend: tests/e2e/seed-test-data.py
cursor.execute("""
    INSERT OR REPLACE INTO users (id, email, password_hash, ...)
    VALUES (?, ?, ?, ...)
""", (1, "test@example.com", bcrypt_hash, ...))

Solution Options

Option A: Custom BetterAuth Adapter (Recommended)

Effort: ~4 hours
Approach: Create a custom database adapter that makes BetterAuth use CodeFRAME's existing tables

Pros:

  • Keeps BetterAuth features (OAuth, 2FA, etc.)
  • Single source of truth for user data
  • Minimal frontend changes

Cons:

  • Requires understanding BetterAuth internals
  • Adapter maintenance burden

Option B: Replace BetterAuth with CodeFRAME Auth

Effort: ~6 hours
Approach: Remove BetterAuth, use CodeFRAME's auth API directly in LoginForm.tsx

Pros:

  • Complete control over auth flow
  • Simpler architecture
  • No third-party dependency

Cons:

  • Lose BetterAuth features
  • More frontend API integration code
  • Need to implement OAuth manually if needed later

Option C: Migrate CodeFRAME to BetterAuth Schema

Effort: ~8 hours
Approach: Change backend to use BetterAuth's schema, migrate existing users

Pros:

  • Standardized auth solution
  • Full BetterAuth feature set
  • Good documentation

Cons:

  • Breaking change for existing deployments
  • Migration complexity
  • Backend tightly coupled to frontend choice

Acceptance Criteria

  • Single authentication system (frontend + backend aligned)
  • E2E tests validate login flow via UI (no session bypass)
  • Existing test user (test@example.com) can login through the UI
  • tests/e2e/auth-bypass.ts deleted (no longer needed)
  • All E2E tests pass using real login flow
  • No [BetterAuthError] in logs

Current Workaround

E2E tests use setTestUserSession() to bypass login by setting session cookies directly. This works for testing post-login flows but doesn't validate the login UI.

Migration path documented in test files:

// Replace setTestUserSession() with loginUser() after auth alignment
test.beforeEach(async ({ page }) => {
  await loginUser(page); // Will work once auth is fixed
});

Related Files

  • web-ui/src/lib/auth.ts - BetterAuth configuration
  • web-ui/src/components/auth/LoginForm.tsx - Login UI (uses BetterAuth)
  • codeframe/persistence/database.py - CodeFRAME users/sessions tables
  • tests/e2e/auth-bypass.ts - Temporary bypass (delete after fix)
  • tests/e2e/test_auth_flow.spec.ts - Auth tests (1/4 passing)

Priority

P2 - High: Blocks full E2E test coverage and creates security/maintenance debt, but workaround exists for core user journey tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0-blocker-betaCritical blocker - must fix before beta testing

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions