Skip to content

Oauth refactor - #505

Merged
tonychang04 merged 8 commits into
mainfrom
oauth-refactor
Oct 22, 2025
Merged

tonychang04 merged 8 commits into
mainfrom
oauth-refactor

Conversation

@tonychang04

@tonychang04 tonychang04 commented Oct 22, 2025 •

Copy link
Copy Markdown
Member

Summary

How did you test this change?

Summary by CodeRabbit

  • New Features

    • Consolidated OAuth flow with unified provider handling and shared callback mechanism
    • Origin-based security validation for OAuth redirects with configurable allowed origins
    • Secure cookie-based token management replacing URL-parameter token passing
  • Configuration

    • Added new environment variables for OAuth configuration: OAUTH_ALLOWED_ORIGINS and DEFAULT_FRONTEND_ORIGIN

Laaaaksh and others added 8 commits October 22, 2025 14:52
- Replace hardcoded provider routes with unified wildcard endpoint
- Implement generic OAuth methods using OAuthProvidersSchema
- Add comprehensive error handling for unsupported providers
- Remove repetitive code and improve maintainability
- Fix route ordering to prevent wildcard route conflicts
- Enhance type safety across frontend and backend
- Add dynamic provider management in frontend
- Remove noisy else statements for cleaner code"
feat: refactor OAuth system for scalability and type safety
@tonychang04
tonychang04 marked this pull request as ready for review October 22, 2025 18:56
@coderabbitai

coderabbitai Bot commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Consolidates provider-specific OAuth flows into a provider-agnostic flow with signed JWT state, origin validation via OAUTH_ALLOWED_ORIGINS/DEFAULT_FRONTEND_ORIGIN, secure httpOnly cookie delivery of tokens, schema/type updates to use OAuthProvidersSchema, and expanded OAuth config fields (id, createdAt, updatedAt).

Changes

Cohort / File(s) Summary
Environment & Compose
\.env\.example, docker-compose\.yml
Add OAUTH_ALLOWED_ORIGINS and DEFAULT_FRONTEND_ORIGIN env vars and example values for redirect origin validation and fallback.
OAuth Routes
backend/src/api/routes/auth.oauth.ts
Replace provider-specific endpoints with /:provider and shared callback flow; validate/normalize origins; sign state with JWT; set secure httpOnly access_token cookie; centralized error handling.
Auth Core
backend/src/core/auth/auth.ts
Add generateAuthUrl(provider, state) and handleOAuthCallback(provider, payload) public APIs; unify provider callbacks under generic handlers; ensure auth URL generation returns strings.
OAuth Data Layer
backend/src/core/auth/oauth.ts
Constrain provider to OAuthProvidersSchema; return id, createdAt, updatedAt from queries; expand default scopes list and adjust update/insert flows.
Seeding
backend/src/utils/seed.ts
Generalize OAuth seeding to loop over provider list/env mappings; add discord/linkedin entries; create configs only when creds exist.
Shared Schemas
shared-schemas/src/auth.schema.ts, shared-schemas/src/auth-api.schema.ts
Change provider to oAuthProvidersSchema; add id (UUID), createdAt, updatedAt to oAuthConfigSchema; adjust create/update request schemas to omit metadata and support partial updates.
Frontend — Components & Hooks
frontend/src/features/auth/components/AddOAuthDialog.tsx, frontend/src/features/auth/components/AuthMethodTab.tsx, frontend/src/features/auth/components/OAuthConfigDialog.tsx, frontend/src/features/auth/hooks/useOAuthConfig.ts
Replace hard-coded provider unions with OAuthProvidersSchema; update prop/state types and runtime provider iteration; introduce sharedKeyProviders whitelist in dialog.
Manifest
frontend/package.json
Bump devDependency @types/react ^19.1.8 → ^19.2.2.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Frontend
    participant Backend
    participant AuthService
    participant Provider as "OAuth Provider"

    User->>Frontend: Click "Connect [Provider]"
    Frontend->>Backend: GET /api/auth/oauth/:provider?origin=...
    Backend->>Backend: validateRedirectOrigin(origin)
    Backend->>Backend: sign state JWT (includes origin)
    Backend->>AuthService: generateAuthUrl(provider, state)
    AuthService-->>Backend: return provider auth URL
    Backend->>Provider: Redirect user to provider auth URL
    Provider->>Backend: Callback to /api/auth/oauth/shared/callback/:state (code/token)
    Backend->>Backend: verify JWT state → extract origin
    Backend->>AuthService: handleOAuthCallback(provider, payload)
    AuthService-->>Backend: CreateSessionResponse (user,tokens)
    Backend->>Backend: set secure httpOnly cookie (access_token)
    Backend->>Frontend: Redirect to extracted origin (no token in URL)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • Fermionic-Lyu

Poem

🐇 I hopped through routes and signed the state,
Origins checked before redirecting fate,
Cookies tucked safe — no token display,
Providers now speak one unified way. ✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch oauth-refactor

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 99d7440 and a4fe151.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (13)
  • .env.example (1 hunks)
  • backend/src/api/routes/auth.oauth.ts (5 hunks)
  • backend/src/core/auth/auth.ts (4 hunks)
  • backend/src/core/auth/oauth.ts (6 hunks)
  • backend/src/utils/seed.ts (4 hunks)
  • docker-compose.yml (1 hunks)
  • frontend/package.json (1 hunks)
  • frontend/src/features/auth/components/AddOAuthDialog.tsx (3 hunks)
  • frontend/src/features/auth/components/AuthMethodTab.tsx (4 hunks)
  • frontend/src/features/auth/components/OAuthConfigDialog.tsx (2 hunks)
  • frontend/src/features/auth/hooks/useOAuthConfig.ts (2 hunks)
  • shared-schemas/src/auth-api.schema.ts (1 hunks)
  • shared-schemas/src/auth.schema.ts (1 hunks)

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants