Skip to content

enforce backend auth truth and prevent stale sessions#407

Open
indresh404 wants to merge 5 commits into
Priyanshu-byte-coder:mainfrom
indresh404:fix/user-sync-and-github-api-errors
Open

enforce backend auth truth and prevent stale sessions#407
indresh404 wants to merge 5 commits into
Priyanshu-byte-coder:mainfrom
indresh404:fix/user-sync-and-github-api-errors

Conversation

@indresh404
Copy link
Copy Markdown
Contributor

Summary

Adds backend-backed auth session validation to prevent stale or mismatched authenticated states in the client.

This PR introduces a new /api/auth/me endpoint to verify session-user mapping, adds startup auth validation with automatic sign-out for invalid sessions, improves GitHub account token lookup reliability, and adds reload buttons for better recovery UX.

Closes #348

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / code cleanup

Changes Made

  • Added /api/auth/me endpoint to validate authenticated sessions against backend user records
  • Ensures invalid or missing user mappings return proper 401 / 404 responses
  • Added AuthSessionValidator component for startup auth verification
  • Mounted validator globally in providers.tsx
  • Automatically signs out users with stale or invalid auth sessions
  • Replaced brittle .single() usage with .maybeSingle() in GitHub account token lookup
  • Reduced PGRST116-style cascading failures from missing user rows
  • Added reload buttons for easier recovery and improved user experience during auth/data mismatch states

How to Test

Steps for the reviewer to verify this works:

  1. Sign in normally and verify authenticated flows still work
  2. Delete or invalidate the corresponding backend user record/session mapping
  3. Refresh the application
  4. Verify the client automatically signs out instead of remaining falsely authenticated
  5. Verify /api/auth/me returns proper 401 or 404 responses for invalid states
  6. Test GitHub account token retrieval and confirm missing rows no longer throw .single() errors
  7. Verify reload buttons properly refresh/recover affected UI states

Checklist

  • Linked issue in summary
  • npm run lint passes locally
  • No TypeScript errors (npm run type-check)
  • Self-reviewed the diff
  • Added/updated tests if applicable

@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

@indresh404 is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Owner

@Priyanshu-byte-coder Priyanshu-byte-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1. 401 → 404 regression — when !session.githubId the missing GitHub ID is an auth problem, not a missing resource. Keep 401. Returning 404 is semantically wrong and will confuse API consumers.

2. signOut() fires on network errorsAuthSessionValidator calls signOut() in the catch block unconditionally. A transient network timeout will log users out. Only call signOut() on HTTP 401/404 responses, not on fetch exceptions.

3. Duplicated GitHubApiError class — the same class and helper are copy-pasted identically into 4 route files. Extract into src/lib/github-error.ts and import.

@github-actions github-actions Bot added gssoc26 GSSoC 2026 contribution type:security GSSoC type bonus: security (+20 pts) labels May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc26 GSSoC 2026 contribution level:advanced GSSoC: Advanced difficulty (55 pts) type:security GSSoC type bonus: security (+20 pts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Missing users Row Causes Auth/API Failures + Hidden GitHub API Errors

2 participants