fix: resolve infinite redirect loops in auth flows - #64
Open
dylan-klein wants to merge 2 commits into
Open
Conversation
- Fixes a redirect loop in service_account mode when anonymous session creation fails by rendering a static HTML error page instead of redirecting to the landing page. - Fixes a dashboard lock-in bug in user_oauth mode when the Google OAuth token expires but the BetterAuth session remains valid. requireSession now verifies Google token validity and clears cookies early if invalid, allowing navigation back to / for re-authentication. - Added integration and unit tests for the updated auth routing behaviors. TAG=agy CONV=b3471264-2592-4924-b031-267b4bf62326
…nt redirect loops - Restored client-side redirect to root page on unauthenticated session in user_oauth mode. - Added explicit authClient.signOut() call before redirecting to ensure local and database-level session cookies are destroyed, preventing concurrent get-session calls from resurrecting the stale cookie. TAG=agy CONV=b3471264-2592-4924-b031-267b4bf62326
dylan-klein
force-pushed
the
fix/redirect-loops
branch
from
August 4, 2026 22:26
8a54e10 to
a7c9451
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.
Motivation
Users returning to Pocket CEP the next day (or after a long period of inactivity) were encountering infinite redirect loops in two scenarios:
/?error=session_unavailable, but since the app was in service account mode, the middleware would immediately redirect back to/api/auth/auto-sessionto try again, causing a loop./dashboard, the APIs would return 401s, but because they had a valid BetterAuth session, they couldn't navigate back to/to re-authenticate (the middleware would redirect them back to/dashboard), locking them in.Main Changes
renderSaSessionErrorHtml) when anonymous session creation fails in/api/auth/auto-session. This breaks the loop and shows diagnostics.requireSessioninsrc/lib/session.tsto verify Google Access Token validity if the app is inuser_oauthmode. If the token is missing or expired, it automatically deletes the BetterAuth session cookies and returnsnull(401). This allows the middleware to redirect the user to/for re-authentication./api/auth/healthto useunauthenticatedResponse()instead of a generic JSON error when session validation fails, ensuring consistent client-side error propagation.src/__tests__/unit/session.test.tsto cover token expiration and cookie clearing inuser_oauthmode.src/__tests__/integration/api/auto-session-route.test.tsto assert that failures in auto-session route return 503 HTML instead of redirecting.src/__tests__/integration/api/auth-health-route.test.tsto cover bothservice_accountanduser_oauthmodes.Design Decisions
requireSession(server-side) to ensure that any page or API route that requires a session will clean up stale state instantly, avoiding complex client-side redirect handling.npm run doctor) is better than JSON.