Skip to content

Commit c5e2058

Browse files
committed
Handle non-JSON responses in JWT validation middleware
Updated the ValidateJwt middleware to return a plain text 'Unauthorized' response for non-JSON requests, while preserving the JSON error response for requests expecting JSON. This improves compatibility with clients that do not expect JSON responses.
1 parent d7a7c4a commit c5e2058

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Http/Middleware/ValidateJwt.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public function handle(Request $request, Closure $next): Response
4848
$request->setUserResolver(fn () => $user);
4949

5050
} catch (\Throwable $e) {
51-
return response()->json(['error' => 'Invalid token', 'message' => $e->getMessage()], Response::HTTP_UNAUTHORIZED);
51+
if ($request->expectsJson()) {
52+
return response()->json(['error' => 'Invalid token', 'message' => $e->getMessage()], Response::HTTP_UNAUTHORIZED);
53+
}
54+
return response('Unauthorized', Response::HTTP_UNAUTHORIZED);
5255
}
5356

5457
return $next($request);

0 commit comments

Comments
 (0)