Open
Conversation
Better Auth's per-key rate limiting was causing misleading 401 errors when users exceeded the 4 req/sec limit. The error said 'Authentication required' instead of indicating rate limiting. Rate limiting is now handled solely by Upstash Redis in the E2 middleware (app/api/e2/lib/auth.ts) which: - Returns proper 429 status codes - Includes standard rate limit headers (X-RateLimit-*, Retry-After) - Uses sliding window algorithm (10 req/sec per user) - Doesn't require DB writes on every request Fixes intermittent 401 errors reported when deleting email addresses via API.
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Summary
Problem
Users reported intermittent 401 "Authentication required" errors when making API calls (e.g., deleting email addresses). The actual cause was hitting Better Auth's rate limit (4 req/sec per key), but the error message was misleading.
Root cause: Better Auth's
verifyApiKey()returns{ valid: false }when rate-limited, and our E2 middleware treated all invalid responses as auth failures → 401.Solution
Disable Better Auth rate limits entirely. Rate limiting is already handled by Upstash Redis in
app/api/e2/lib/auth.tswhich:X-RateLimit-*,Retry-AfterChanges
lib/auth/auth.ts: SetrateLimit.enabled: falsein apiKey plugin configTesting
Note
Medium Risk
Touches API key authentication behavior; disabling built-in throttling relies on external middleware being correctly configured, which could affect protection against abuse if misconfigured.
Overview
Disables Better Auth’s per-API-key rate limiting by setting
apiKey().rateLimit.enabledtofalseinlib/auth/auth.ts.Adds inline comments clarifying that rate limiting is handled elsewhere (Upstash in E2 middleware) to avoid duplicate limits and to return proper
429responses instead of misleading401s.Written by Cursor Bugbot for commit b9de77e. This will update automatically on new commits. Configure here.