Skip to content

fix(ai): require auth, rate limit and credits on AI endpoints (#69) - #72

Merged
MistryVishwa merged 1 commit into
MistryVishwa:mainfrom
Rudra-clrscr:fix/69-ai-endpoints-auth-ratelimit-credits
Jul 6, 2026
Merged

fix(ai): require auth, rate limit and credits on AI endpoints (#69)#72
MistryVishwa merged 1 commit into
MistryVishwa:mainfrom
Rudra-clrscr:fix/69-ai-endpoints-auth-ratelimit-credits

Conversation

@Rudra-clrscr

Copy link
Copy Markdown
Contributor

Closes #69

Problem

Five paid AI-generation routes called Gemini / Groq / Tavily before
verifying authentication (or without any auth at all). Anyone without an
account could repeatedly hit them and drain the app's API quota. The existing
rateLimit() and credit helpers were defined but never used.

Fix

Added a shared guard lib/ai-guard.tsrequireAiAccess() that runs
before any external API call and, in order:

  1. 401 if the request is unauthenticated (getUser())
  2. 429 if the per-user burst limit is exceeded (existing rateLimit())
  3. 402 if the user is out of credits (existing consumeCredit(), ai_chat)

On success it returns the authenticated user; on failure it returns a
ready-to-send NextResponse, so each route just does:

const guard = await requireAiAccess()
if (guard.error) return guard.error
const { user } = guard

…Vishwa#69)

The five paid AI-generation routes (chat, chat/explain, image, transcribe,
notes) invoked Gemini / Groq / Tavily before verifying authentication, so
anyone without an account could drain the app's API quota indefinitely.

Add a shared requireAiAccess() guard (lib/ai-guard.ts) that runs before any
external API call and:
  - rejects unauthenticated requests with 401
  - applies the existing rateLimit() helper (429 on burst)
  - consumes a credit via the existing consumeCredit() helper (402 when out)

Auth checks that previously ran after the AI call are removed in favour of the
up-front guard.
@vercel

vercel Bot commented Jul 5, 2026

Copy link
Copy Markdown

@Rudra-clrscr is attempting to deploy a commit to the vishwamistrylearning-1037's projects Team on Vercel.

A member of the Team first needs to authorize it.

@MistryVishwa MistryVishwa added bug Something isn't working good first issue Good for newcomers ssoc26 Main tag identifying the repository for Social Summer of Code 2026 Medium 30 Points labels Jul 6, 2026
@MistryVishwa
MistryVishwa merged commit 36af3a2 into MistryVishwa:main Jul 6, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working good first issue Good for newcomers Medium 30 Points ssoc26 Main tag identifying the repository for Social Summer of Code 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] AI generation endpoints have no authentication, rate limiting, or credit checks (cost-abuse risk)

2 participants