Fix SSR auth for private repository pages#338
Open
manana2520 wants to merge 2 commits intoAIDotNet:mainfrom
Open
Fix SSR auth for private repository pages#338manana2520 wants to merge 2 commits intoAIDotNet:mainfrom
manana2520 wants to merge 2 commits intoAIDotNet:mainfrom
Conversation
Private repos returned "Repository Not Found" on direct navigation because SSR fetches had no Authorization header. JWT was stored only in localStorage, which is inaccessible during server-side rendering. - Store JWT in cookie alongside localStorage on login/logout - Add getServerToken() to read cookie via next/headers during SSR - Add getSSRAuthHeaders() helper in repository-api.ts - Inject auth headers in all SSR fetch functions (fetchRepoTree, fetchRepoBranches, fetchRepoDoc, checkGitHubRepo, fetchRepoStatus, fetchProcessingLogs, fetchRepositoryList, fetchGitBranches, fetchMindMap) On client side, getServerToken() returns null (window exists) so client-side auth continues to work via apiClient and localStorage as before. No backend changes needed - backend already handles Bearer tokens correctly.
getServerToken() was calling cookies() synchronously, but in Next.js 15+ cookies() returns a Promise. The function got a Promise object instead of the cookie store, so the token was never read during SSR. - Make getServerToken() async with await cookies() - Make getSSRAuthHeaders() async - Await getSSRAuthHeaders() in all fetch call sites
90bd133 to
c7e96d6
Compare
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
Private repositories show "Repository Not Found" when users navigate directly to a repo URL (e.g.,
/owner/repo) because Next.js Server Components call fetch without an Authorization header. The JWT token was stored only inlocalStorage, which is inaccessible during SSR.Changes
web/lib/auth-api.tsdeepwiki_token) alongsidelocalStorageon logingetServerToken()that reads the cookie vianext/headersduring SSRweb/lib/repository-api.tsgetSSRAuthHeaders()helper that returnsAuthorization: Bearer <token>if a cookie is presentfetchRepoTree,fetchRepoBranches,fetchRepoDoc,checkGitHubRepo,fetchRepoStatus,fetchProcessingLogs,fetchRepositoryList,fetchGitBranches,fetchMindMapHow it works
setToken()sets bothlocalStorageand cookie/owner/private-repo-> SSR callsfetchRepoTree()getSSRAuthHeaders()callsgetServerToken()which reads cookie vianext/headersAuthorization: Bearer <jwt>header -> backend returns repo datagetServerToken()returns null (window exists), so client-side auth continues viaapiClient+localStorageas beforeTest plan