Skip to content

fix: prevent redirectUsersTo from overwriting guest redirect callback#59585

Closed
aydinza wants to merge 1 commit intolaravel:13.xfrom
aydinza:fix/redirect-guests-overwrite
Closed

fix: prevent redirectUsersTo from overwriting guest redirect callback#59585
aydinza wants to merge 1 commit intolaravel:13.xfrom
aydinza:fix/redirect-guests-overwrite

Conversation

@aydinza
Copy link
Copy Markdown

@aydinza aydinza commented Apr 7, 2026

Problem

When redirectGuestsTo() and redirectUsersTo() are called seperately in bootstrap/app.php:

$middleware->redirectGuestsTo(fn (Request $request) => route('login'));
$middleware->redirectUsersTo(fn (Request $request) => route('dashboard'));

The second call (redirectUsersTo) somehow overwrites the guest redirect callback, causing unauth requests to receive a 401 response instead of being redirected to the login page.

Root cause

In Middleware::redirectTo(), the $guests parameter defaults to null. The condition on line 564:

$guests = is_string($guests) || is_null($guests) ? fn () => $guests : $guests;

wraps null into fn () => null — a truth closure — which passes the if ($guests) guard and overwrites the previously configured guest redirect callback with one that returns null.

The exception handler then receives null from $exception->redirectTo() and returns response()->noContent(401) instead of redirecting.

Workaround

Using a single redirectTo() call with both parameters avoids the issue:

$middleware->redirectTo(
    guests: fn (Request $request) => route('login'),
    users: fn (Request $request) => route('dashboard'),
);

Solution

Remove is_null() from the aforementioned condition so that a default null parameter stays null and is naturally skipped by the if ($guests) guard:

$guests = is_string($guests) ? fn () => $guests : $guests;

Tests

Added two tests:

  • testRedirectUsersToDoesNotOverwriteRedirectGuestsTo — verifies the Authenticate and AuthenticationException callbacks remain intact after calling both redirectGuestsTo() and redirectUsersTo().
  • testRedirectGuestsToWithCallable — verifies callable-based guest redirect works correctly alongside redirectUsersTo().

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.
@aydinza
Copy link
Copy Markdown
Author

aydinza commented Apr 7, 2026

looks like the fix landed in 451fd57 — same one-liner we proposed here. glad it's resolved! 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants