90f9857b06
API CI/CD / Validate (composer + pint) (push) Successful in 3m7s
API CI/CD / Test (PHPUnit) (push) Failing after 5m46s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
|
|
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class ApiJwtAuth
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
try {
|
|
$user = JWTAuth::parseToken()->authenticate();
|
|
if (! $user) {
|
|
return response()->json(['status' => false, 'message' => 'Unauthorized.'], 401);
|
|
}
|
|
|
|
Auth::setUser($user);
|
|
|
|
if ($response = $this->denyUnavailableAccount($user)) {
|
|
return $response;
|
|
}
|
|
} catch (JWTException $e) {
|
|
return response()->json(['status' => false, 'message' => 'Unauthorized.'], 401);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
private function denyUnavailableAccount(object $user): ?Response
|
|
{
|
|
$status = strtolower(trim((string) ($user->status ?? '')));
|
|
$isVerified = filter_var($user->is_verified ?? false, FILTER_VALIDATE_BOOLEAN);
|
|
$isSuspended = filter_var($user->is_suspended ?? false, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
if (! $isVerified || $status !== 'active' || $isSuspended) {
|
|
return response()->json(['message' => 'Account unavailable.'], 403);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|