Skip to content

Commit ea3d479

Browse files
nunomaduroStyleCIBottaylorotwell
authored
[11.x] Improves RedirectIfAuthenticated middleware default redirect (#49286)
* Improves `RedirectIfAuthenticated` * Apply fixes from StyleCI * Improves sorting * Improves perf * formatting --------- Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2526dbf commit ea3d479

File tree

3 files changed

+134
-4
lines changed

3 files changed

+134
-4
lines changed

src/Illuminate/Auth/Middleware/RedirectIfAuthenticated.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
use Closure;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\Auth;
8+
use Illuminate\Support\Facades\Route;
89
use Symfony\Component\HttpFoundation\Response;
910

1011
class RedirectIfAuthenticated
1112
{
1213
/**
1314
* The callback that should be used to generate the authentication redirect path.
1415
*
15-
* @var callable
16+
* @var callable|null
1617
*/
1718
protected static $redirectToCallback;
1819

@@ -41,7 +42,29 @@ protected function redirectTo(Request $request): ?string
4142
{
4243
return static::$redirectToCallback
4344
? call_user_func(static::$redirectToCallback, $request)
44-
: '/dashboard';
45+
: $this->defaultRedirectUri();
46+
}
47+
48+
/**
49+
* Get the default URI the user should be redirected to when they are authenticated.
50+
*/
51+
protected function defaultRedirectUri(): string
52+
{
53+
foreach (['dashboard', 'home'] as $uri) {
54+
if (Route::has($uri)) {
55+
return route($uri);
56+
}
57+
}
58+
59+
$routes = Route::getRoutes()->get('GET');
60+
61+
foreach (['dashboard', 'home'] as $uri) {
62+
if (isset($routes[$uri])) {
63+
return '/'.$uri;
64+
}
65+
}
66+
67+
return '/';
4568
}
4669

4770
/**

src/Illuminate/Foundation/Configuration/ApplicationBuilder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ public function withMiddleware(callable $callback)
191191
{
192192
$this->app->afterResolving(HttpKernel::class, function ($kernel) use ($callback) {
193193
$middleware = (new Middleware)
194-
->auth(redirectTo: fn () => route('login'))
195-
->guest(redirectTo: fn () => route('dashboard'));
194+
->auth(redirectTo: fn () => route('login'));
196195

197196
$callback($middleware);
198197

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Auth\Middleware;
4+
5+
use Illuminate\Auth\Middleware\RedirectIfAuthenticated;
6+
use Illuminate\Contracts\Routing\Registrar;
7+
use Illuminate\Support\Str;
8+
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
9+
use Orchestra\Testbench\Factories\UserFactory;
10+
use Orchestra\Testbench\TestCase;
11+
12+
class RedirectIfAuthenticatedTest extends TestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->withoutExceptionHandling();
19+
20+
/** @var \Illuminate\Contracts\Routing\Registrar $router */
21+
$this->router = $this->app->make(Registrar::class);
22+
23+
$this->router->get('/login', function () {
24+
return response('Login Form');
25+
})->middleware(RedirectIfAuthenticated::class);
26+
27+
UserFactory::new()->create();
28+
29+
$user = AuthenticationTestUser::first();
30+
$this->router->get('/login', function () {
31+
return response('Login Form');
32+
})->middleware(RedirectIfAuthenticated::class);
33+
34+
UserFactory::new()->create();
35+
36+
$this->user = AuthenticationTestUser::first();
37+
}
38+
39+
protected function defineEnvironment($app)
40+
{
41+
$app['config']->set('app.key', Str::random(32));
42+
$app['config']->set('auth.providers.users.model', AuthenticationTestUser::class);
43+
}
44+
45+
protected function defineDatabaseMigrations()
46+
{
47+
$this->loadLaravelMigrations();
48+
}
49+
50+
public function testWhenDashboardNamedRouteIsAvailable()
51+
{
52+
$this->router->get('/named-dashboard', function () {
53+
return response('Named Dashboard');
54+
})->name('dashboard');
55+
56+
$response = $this->actingAs($this->user)->get('/login');
57+
58+
$response->assertRedirect('/named-dashboard');
59+
}
60+
61+
public function testWhenHomeNamedRouteIsAvailable()
62+
{
63+
$this->router->get('/named-home', function () {
64+
return response('Named Home');
65+
})->name('home');
66+
67+
$response = $this->actingAs($this->user)->get('/login');
68+
69+
$response->assertRedirect('/named-home');
70+
}
71+
72+
public function testWhenDashboardSlugIsAvailable()
73+
{
74+
$this->router->get('/dashboard', function () {
75+
return response('My Dashboard');
76+
});
77+
78+
$response = $this->actingAs($this->user)->get('/login');
79+
80+
$response->assertRedirect('/dashboard');
81+
}
82+
83+
public function testWhenHomeSlugIsAvailable()
84+
{
85+
$this->router->get('/home', function () {
86+
return response('My Home');
87+
})->name('home');
88+
89+
$response = $this->actingAs($this->user)->get('/login');
90+
91+
$response->assertRedirect('/home');
92+
}
93+
94+
public function testWhenHomeOrDashboardAreNotAvailable()
95+
{
96+
$response = $this->actingAs($this->user)->get('/login');
97+
98+
$response->assertRedirect('/');
99+
}
100+
101+
public function testWhenGuest()
102+
{
103+
$response = $this->get('/login');
104+
105+
$response->assertOk();
106+
$response->assertSee('Login Form');
107+
}
108+
}

0 commit comments

Comments
 (0)