Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LogoutAction | |
100.00% |
14 / 14 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Auth; |
| 6 | |
| 7 | use App\Domain\Auth\Service\AuthService; |
| 8 | use App\Domain\Exception\BadRequestException; |
| 9 | use App\Support\Row; |
| 10 | use Psr\Http\Message\ResponseInterface; |
| 11 | use Psr\Http\Message\ServerRequestInterface; |
| 12 | |
| 13 | /** |
| 14 | * Logout user. |
| 15 | * |
| 16 | * POST /api/auth/logout |
| 17 | * Requires: JWT authentication |
| 18 | */ |
| 19 | final class LogoutAction |
| 20 | { |
| 21 | private AuthService $authService; |
| 22 | |
| 23 | public function __construct(AuthService $authService) |
| 24 | { |
| 25 | $this->authService = $authService; |
| 26 | } |
| 27 | |
| 28 | public function __invoke( |
| 29 | ServerRequestInterface $request, |
| 30 | ResponseInterface $response, |
| 31 | ): ResponseInterface { |
| 32 | // Get request data |
| 33 | $data = (array)$request->getParsedBody(); |
| 34 | |
| 35 | // Validate required fields |
| 36 | $refreshToken = Row::nullableString($data, 'refreshToken'); |
| 37 | if ($refreshToken === null || $refreshToken === '') { |
| 38 | throw new BadRequestException('Missing required field: refreshToken'); |
| 39 | } |
| 40 | |
| 41 | // logout (revoke refresh token) |
| 42 | $this->authService->logout($refreshToken); |
| 43 | |
| 44 | // Return success response |
| 45 | $responseData = [ |
| 46 | 'success' => true, |
| 47 | 'message' => 'Logout successful', |
| 48 | ]; |
| 49 | |
| 50 | $response->getBody()->write((string)json_encode($responseData)); |
| 51 | |
| 52 | return $response |
| 53 | ->withHeader('Content-Type', 'application/json') |
| 54 | ->withStatus(200); |
| 55 | } |
| 56 | } |