fix: return JSON 401 for unauthenticated API requests instead of redirecting to login route

This commit is contained in:
li
2026-03-14 17:57:27 +08:00
parent f7eaedd022
commit 0f721bcc09
+5 -1
View File
@@ -22,5 +22,9 @@ return Application::configure(basePath: dirname(__DIR__))
// Removed statefulApi() — app uses Bearer token auth, not cookie SPA auth
})
->withExceptions(function (Exceptions $exceptions): void {
//
$exceptions->render(function (\Illuminate\Auth\AuthenticationException $e, $request) {
if ($request->expectsJson() || $request->is('api/*')) {
return response()->json(['message' => '未认证,请先登录'], 401);
}
});
})->create();