API Authentication Updates (2.0 edition)#32
Merged
0xGingi merged 3 commits intonanogpt-community:mainfrom Jan 9, 2026
thejudge22:api-updates
Merged
API Authentication Updates (2.0 edition)#320xGingi merged 3 commits intonanogpt-community:mainfrom thejudge22:api-updates
0xGingi merged 3 commits intonanogpt-community:mainfrom
thejudge22:api-updates
Conversation
added 3 commits
January 8, 2026 22:04
…nd missing endpoints
- Create src/lib/backend/auth-utils.ts with shared authentication helpers:
- getAuthenticatedUserId(request): Tries API key first, falls back to session
- tryGetAuthenticatedUserId(request): Same but returns undefined instead of throwing
- getUserIdFromApiKey(authHeader): Validates Bearer token and returns userId
- Update 28 endpoint files to use dual authentication:
- Assistants: GET, POST, PATCH, DELETE, set-default
- Projects: GET, POST, PATCH, DELETE, files, members endpoints
- DB endpoints: conversations, messages, message-interactions, message-ratings,
user-settings, user-keys, user-rules, user-models, model-performance
- Storage: POST, DELETE
- Utilities: cancel-generation, enhance-prompt, cleanup-temp-conversations,
generate-follow-up-questions
- NanoGPT: balance, subscription-usage, model-providers, provider-preferences
- User: upload-avatar, karakeep/save-chat, api-keys
- Update api-docs.md to reflect dual auth support for all updated endpoints
Excluded endpoints (intentionally):
- TTS/STT/Video: Use x-api-key header for NanoGPT, optional session for analytics
- /api/storage/[id]: Public file serving
- /api/auth/*: Auth handler
- /api/artificial-analysis/benchmarks: Public endpoint
- /api/generate-message: Already had dual auth (reference implementation)
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.
Add Dual Authentication (Session + API Key) to All API Endpoints
Summary
This PR adds dual authentication support to all API endpoints, allowing users to authenticate using either session cookies (existing web UI behavior) or API keys via Bearer tokens (
Authorization: Bearer nc_...).Changes
New File
src/lib/backend/auth-utils.ts- Shared authentication utilitiesgetAuthenticatedUserId(request)- Tries API key first, falls back to session cookietryGetAuthenticatedUserId(request)- Returns undefined instead of throwing for optional authgetUserIdFromApiKey(authHeader)- Validates Bearer token and returns userIdUpdated Endpoints (28 files)
/api/assistants• PATCH, DELETE, POST/api/assistants/[id]/api/projects,/api/projects/[id],/api/projects/[id]/files,/api/projects/[id]/members/api/storageDocumentation
api-docs.md- Changed 46+ endpoint authentication labels to "Session or API Key"Excluded Endpoints (Intentional)
/api/tts,/api/stt,/api/video/*x-api-keyfor NanoGPT API, optional session for analytics/api/storage/[id]/api/artificial-analysis/benchmarks/api/generate-message/api/auth/*Testing
Verification
npm run buildpasses