Clear the session cookie when a token refresh fails so sign-in doesn't loop#29
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes an auth redirect loop that occurred when a GitHub token refresh fails (e.g., expired refresh token) by ensuring the auth middleware clears the session cookie before re-throwing the /signin?reason=session_expired redirect. This turns the expired session into a clean logout, preventing ERR_TOO_MANY_REDIRECTS.
Changes:
- Update
authMiddlewareto catch token-refresh redirects and re-throw them withdestroySessionapplied viaSet-Cookie. - Add a regression test to verify the session cookie is cleared on
session_expiredredirects and that non-redirect refresh errors still propagate.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
app/middleware/auth.server.ts |
Wraps maybeRefreshToken to re-throw redirect responses with a destroyed session cookie to avoid redirect loops. |
tests/auth-middleware-session-expired.test.ts |
Adds regression coverage for cookie-clearing behavior on refresh redirect and error propagation on non-redirect failures. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
When a GitHub token refresh fails or the refresh token has expired,
maybeRefreshTokenredirects to/signin?reason=session_expired. The auth middleware was letting that redirect through without clearing the session cookie. Because the cookie stayed valid, the sign-in loader bounced the user straight back to/dashboard, which failed the refresh again — producing anERR_TOO_MANY_REDIRECTSloop for any signed-in user whose refresh token expired.The middleware now re-throws that redirect with
destroySession, turning an expired session into a clean logout — consistent with the middleware's other two exits. The user lands on the sign-in page and can re-authenticate.Adds a regression test covering both the cookie-clearing redirect and that non-redirect refresh errors still propagate.