Skip to content

Commit d043487

Browse files
committed
Replace custom auth middleware with default auth middleware and a custom redirect
1 parent af0e72c commit d043487

4 files changed

Lines changed: 36 additions & 22 deletions

File tree

app/Http/Middleware/Authenticate.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

bootstrap/app.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
declare(strict_types=1);
44

5-
use App\Http\Middleware\Authenticate;
65
use App\Providers\AppServiceProvider;
76
use Illuminate\Foundation\Application;
87
use Illuminate\Foundation\Configuration\Exceptions;
98
use Illuminate\Foundation\Configuration\Middleware;
9+
use Illuminate\Http\Request;
1010
use KgBot\LaravelLocalization\LaravelLocalizationServiceProvider;
1111

1212
return Application::configure(basePath: dirname(__DIR__))
@@ -23,11 +23,11 @@
2323
->withMiddleware(function (Middleware $middleware) {
2424
$middleware->redirectUsersTo(AppServiceProvider::HOME);
2525

26-
$middleware->throttleApi();
26+
$middleware->redirectGuestsTo(
27+
fn (Request $request) => $request->expectsJson() ? null : route_wlocale('login'),
28+
);
2729

28-
$middleware->alias([
29-
'auth' => Authenticate::class,
30-
]);
30+
$middleware->throttleApi();
3131
})
3232
->withExceptions(function (Exceptions $exceptions) {
3333
//

tests/Feature/AuthTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
uses(TestCase::class);
1010
uses(RefreshDatabase::class);
1111

12+
test('guests are redirected to the locale-prefixed login page', function () {
13+
$this->get('/en/profile')->assertRedirect('/en/login');
14+
});
15+
16+
test('guest json requests receive 401 instead of a redirect', function () {
17+
$this->getJson('/en/profile')->assertUnauthorized();
18+
});
19+
1220
test('user can log in', function () {
1321
$user = User::factory()->create();
1422

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Tests\NovaTestCase;
7+
8+
uses(NovaTestCase::class);
9+
uses(RefreshDatabase::class);
10+
11+
test('guest json requests to nova api receive 401 instead of a url generation error', function () {
12+
$this->getJson('/nova-api/users')->assertUnauthorized();
13+
});
14+
15+
test('guest non-json requests to nova api do not throw a url generation error', function () {
16+
// Regression: the framework's default guest redirect calls route('login')
17+
// without the required {locale} parameter, causing a 500 in production.
18+
$this->get('/nova-api/users')->assertUnauthorized();
19+
});
20+
21+
test('guests requesting nova pages are redirected to the nova login page', function () {
22+
$this->get('/nova')->assertRedirect('/nova/login');
23+
});

0 commit comments

Comments
 (0)