52 lines
2.1 KiB
PHP
52 lines
2.1 KiB
PHP
<?php
|
|
|
|
use App\Http\Middleware\AuthenticateAdminPanelSession;
|
|
use App\Http\Middleware\EnsureUserIsAdmin;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
// Content-management endpoints live in their own files so the public and
|
|
// legacy routes in api.php stay readable. `then:` does not inherit the
|
|
// `api:` prefix or middleware group from the arguments above, so both
|
|
// are applied explicitly here to keep these routes identical in shape
|
|
// to the ones declared in routes/api.php.
|
|
then: function () {
|
|
Route::prefix('api')->middleware('api')->group(base_path('routes/media.php'));
|
|
Route::prefix('api')->middleware('api')->group(base_path('routes/crud.php'));
|
|
},
|
|
)
|
|
->withMiddleware(function (Middleware $middleware) {
|
|
// Let the Next.js admin panel authenticate with a Sanctum session
|
|
// cookie issued on the same registrable domain, instead of shipping a
|
|
// bearer token to JavaScript.
|
|
$middleware->trustProxies(at: '*');
|
|
$middleware->statefulApi();
|
|
|
|
$middleware->api(prepend: [
|
|
AuthenticateAdminPanelSession::class,
|
|
]);
|
|
|
|
$middleware->alias([
|
|
'admin' => EnsureUserIsAdmin::class,
|
|
]);
|
|
|
|
// This is an API-only application with no `login` route. Without this,
|
|
// an unauthenticated write request that does not advertise JSON (a
|
|
// plain form POST or curl) resolves `route('login')` and dies with
|
|
// "Route [login] not defined" instead of a 401.
|
|
$middleware->redirectGuestsTo(fn () => null);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions) {
|
|
$exceptions->shouldRenderJsonWhen(
|
|
fn ($request) => $request->is('api/*') || $request->expectsJson()
|
|
);
|
|
})->create();
|