CaiWuXiTong/app/Http/Middleware/Authenticate.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

49 lines
1.4 KiB
PHP

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
class Authenticate extends Middleware
{
/**
* Handle an incoming request.
*
* When the request carries a Bearer token but authentication fails, return
* an RFC 6750 compliant error payload instead of the generic "Unauthenticated".
*/
public function handle($request, Closure $next, ...$guards)
{
try {
return parent::handle($request, $next, ...$guards);
} catch (AuthenticationException $e) {
if ($request->bearerToken()) {
return response()->json([
'error' => 'invalid_token',
'error_description' => 'The access token provided is expired, revoked, malformed, or invalid.',
], 401, [
'WWW-Authenticate' => 'Bearer error="invalid_token", error_description="Invalid or expired token"',
]);
}
throw $e;
}
}
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (!$request->expectsJson()) {
return route('login');
}
}
}