Files
panel/app/Http/Middleware/AddPasskeyErrorMessage.php
JoanFo a9cb16e042 Passkeys (#2192)
Co-authored-by: Lance Pioch <git@lance.sh>
2026-06-22 23:59:07 -04:00

36 lines
1.1 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class AddPasskeyErrorMessage
{
/**
* The @laravel/passkeys browser client reads the failure reason from a
* top-level "message" field, but Pelican's exception handler renders errors
* in a JSON:API envelope ({"errors": [{"detail": ...}]}) without one. Mirror
* the first error's detail into "message" so passkey failures surface a
* useful reason instead of "Request failed with status 4xx".
*/
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
if ($response instanceof JsonResponse && ($response->isClientError() || $response->isServerError())) {
$data = $response->getData(true);
if (is_array($data) && !isset($data['message']) && isset($data['errors'][0]['detail'])) {
$data['message'] = $data['errors'][0]['detail'];
$response->setData($data);
}
}
return $response;
}
}