Closes Module 1: 9 auth endpoints under /api/v1/auth, OTP via SMS (Semaphore + log + fake drivers), role middleware, role + admin seeders, 27 feature tests passing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
70 lines
2.6 KiB
PHP
70 lines
2.6 KiB
PHP
<?php
|
|
|
|
use App\Http\Responses\ApiResponse;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
apiPrefix: 'api/v1',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
)
|
|
->withMiddleware(function (Middleware $middleware) {
|
|
$middleware->statefulApi();
|
|
|
|
$middleware->alias([
|
|
'role' => \App\Http\Middleware\EnsureUserHasRole::class,
|
|
]);
|
|
|
|
$middleware->redirectGuestsTo(function (Request $request) {
|
|
return $request->is('api/*') ? null : null;
|
|
});
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions) {
|
|
$exceptions->shouldRenderJsonWhen(function (Request $request) {
|
|
return $request->is('api/*') || $request->expectsJson();
|
|
});
|
|
|
|
$exceptions->render(function (ValidationException $e, Request $request) {
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
return ApiResponse::error(
|
|
'Validation failed',
|
|
$e->errors(),
|
|
Response::HTTP_UNPROCESSABLE_ENTITY,
|
|
);
|
|
}
|
|
});
|
|
|
|
$exceptions->render(function (AuthenticationException $e, Request $request) {
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
return ApiResponse::error('Unauthenticated', null, Response::HTTP_UNAUTHORIZED);
|
|
}
|
|
});
|
|
|
|
$exceptions->render(function (NotFoundHttpException $e, Request $request) {
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
return ApiResponse::error('Resource not found', null, Response::HTTP_NOT_FOUND);
|
|
}
|
|
});
|
|
|
|
$exceptions->render(function (HttpExceptionInterface $e, Request $request) {
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
return ApiResponse::error(
|
|
$e->getMessage() ?: 'Request failed',
|
|
null,
|
|
$e->getStatusCode(),
|
|
);
|
|
}
|
|
});
|
|
})->create();
|