Skip to content

fix(auth): Postgres as sole referral source of truth + web client gate - #41

Draft
EpicStarAi wants to merge 2 commits into
mainfrom
fix/referral-auth-postgres-v2
Draft

fix(auth): Postgres as sole referral source of truth + web client gate#41
EpicStarAi wants to merge 2 commits into
mainfrom
fix/referral-auth-postgres-v2

Conversation

@EpicStarAi

Copy link
Copy Markdown
Owner

Root cause

authData.ts swallowed every DB error with a bare catch {} and fell through to a file fallback that had no codes, because EPIC_DEV_REFERRAL was never set. Every referral attempt returned invalid regardless of what code the user entered.

Additionally, the referral_codes table had never been created in PostgreSQL (schema applied lazily on first call), and the Vite web client had no referral gate — /client was unprotected.

Changes

epicgram/apps/web/lib/authData.ts

  • Replace catch {} with proper error propagation when DATABASE_URL is configured.
  • Returns { ok: false, reason: "service_unavailable" } instead of silently falling to file store.
  • File fallback only reached when DATABASE_URL is absent entirely.
  • All three methods: referralLogin, getSession, logout.

epicgram/apps/web/app/gate/page.tsx

  • Added service_unavailable → "База данных недоступна — попробуйте позже".

artifacts/api-server/src/lib/auth-db.ts (new)

  • PostgreSQL auth module: same schema as epicgram/apps/web/lib/authDb.ts.
  • Tables created on first call. Seeds dev code from EPIC_DEV_REFERRAL on first run.
  • Only SHA-256 hashes stored — raw codes/tokens never logged.

artifacts/api-server/src/routes/auth.ts (new)

  • POST /api/auth/referral-login — validates code, issues epic_session httpOnly cookie.
  • GET /api/auth/session — session status.
  • POST /api/auth/logout — expires session + clears cookie.
  • Rate limiter: 8 attempts/IP/minute.

artifacts/epicgram-web/src/pages/Gate.tsx (new)

  • Referral gate UI (dark fuchsia/neon, Russian). On success → /client. On existing session → skips.

artifacts/epicgram-web/src/App.tsx

  • Added /gate route. /client wrapped in ProtectedClient: unauthenticated → /gate.

Test plan

  1. EPIC_DEV_REFERRAL secret set ✅
  2. Open /gate → enter dev referral code → expect redirect to /client.
  3. Open /client directly without session → expect redirect to /gate.
  4. Enter invalid code → "Неверный код".
  5. Logout → session cleared, back to gate.

Draft — do not merge or deploy without owner confirmation.

EPICGRAM Replit and others added 2 commits July 23, 2026 00:09
Full snapshot of Replit workspace main branch (commit 5615a80) integrated onto
GitHub origin/main (4ca2290) to establish shared commit history for review.

Summary of changes vs GitHub main:
  - 440 files added   (new features: artifacts/, epicgram/, packages/, etc.)
  - 85  files deleted (GitHub-only scaffolding replaced by monorepo structure)
  - 3   files modified (tsconfig.json, tsconfig.base.json, pnpm-lock.yaml)

No force push. No production changes. Draft PR only.
Root cause: authData.ts swallowed DB errors silently (catch {}) and
fell through to a file fallback that had no codes, because
EPIC_DEV_REFERRAL was never set — so every referral attempt returned
'invalid' regardless of the code entered.

Changes:
- authData.ts: replace catch{} with proper error propagation when DB
  is configured. Returns reason:'service_unavailable' instead of
  silently falling to the file store. Postgres is the sole source of
  truth when DATABASE_URL is set.
- gate/page.tsx (Next.js): add 'service_unavailable' to the error map.
- artifacts/api-server: add /api/auth/* routes (referral-login,
  session, logout) backed by Postgres. Tables are created on first
  call; EPIC_DEV_REFERRAL seeds one dev code if the table is empty.
- artifacts/epicgram-web: add /gate route (referral gate UI) and
  protect /client with a session check — unauthenticated users are
  redirected to /gate.
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.

1 participant