fix(ai): require auth, rate limit and credits on AI endpoints (#69) - #72
Merged
MistryVishwa merged 1 commit intoJul 6, 2026
Conversation
…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.
|
@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
approved these changes
Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.ts→requireAiAccess()that runsbefore any external API call and, in order:
getUser())rateLimit())consumeCredit(),ai_chat)On success it returns the authenticated user; on failure it returns a
ready-to-send
NextResponse, so each route just does: