fix: prevent redirectUsersTo from overwriting guest redirect callback#59585
Closed
aydinza wants to merge 1 commit intolaravel:13.xfrom
Closed
fix: prevent redirectUsersTo from overwriting guest redirect callback#59585aydinza wants to merge 1 commit intolaravel:13.xfrom
aydinza wants to merge 1 commit intolaravel:13.xfrom
Conversation
When `redirectGuestsTo()` and `redirectUsersTo()` are called separately, the second call invokes `redirectTo(users: ...)` where `$guests` defaults to `null`. The `is_null($guests)` check wraps this into `fn () => null`, a truthy closure that passes the `if ($guests)` guard and overwrites the previously configured guest redirect callback. This causes unauthenticated requests to receive a 401 response instead of being redirected to the login page. The fix removes `is_null()` from the transformation condition so that a default `null` parameter stays `null` and is skipped by the guard.
Author
|
looks like the fix landed in 451fd57 — same one-liner we proposed here. glad it's resolved! 🤝 |
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.
Problem
When
redirectGuestsTo()andredirectUsersTo()are called seperately inbootstrap/app.php:The second call (
redirectUsersTo) somehow overwrites the guest redirect callback, causing unauth requests to receive a401response instead of being redirected to the login page.Root cause
In
Middleware::redirectTo(), the$guestsparameter defaults tonull. The condition on line 564:wraps
nullintofn () => null— a truth closure — which passes theif ($guests)guard and overwrites the previously configured guest redirect callback with one that returnsnull.The exception handler then receives
nullfrom$exception->redirectTo()and returnsresponse()->noContent(401)instead of redirecting.Workaround
Using a single
redirectTo()call with both parameters avoids the issue:Solution
Remove
is_null()from the aforementioned condition so that a defaultnullparameter staysnulland is naturally skipped by theif ($guests)guard:Tests
Added two tests:
testRedirectUsersToDoesNotOverwriteRedirectGuestsTo— verifies the Authenticate and AuthenticationException callbacks remain intact after calling bothredirectGuestsTo()andredirectUsersTo().testRedirectGuestsToWithCallable— verifies callable-based guest redirect works correctly alongsideredirectUsersTo().