add logout endpoint
This commit is contained in:
parent
89f448bd42
commit
22d9ad5136
3 changed files with 42 additions and 0 deletions
|
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
|||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||
use App\Auth\UseCases\CreateSession\CreateSession;
|
||||
use App\Auth\UseCases\Logout\Logout;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Http\Middleware\AuthMiddleware;
|
||||
|
|
@ -19,6 +20,7 @@ class AuthController extends Controller
|
|||
public function __construct(
|
||||
private AuthenticateUser $authenticateUser,
|
||||
private CreateSession $createSession,
|
||||
private Logout $logout,
|
||||
) {}
|
||||
|
||||
public function login(Request $request): JsonResponse
|
||||
|
|
@ -71,6 +73,28 @@ class AuthController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
public function logout(Request $request): JsonResponse
|
||||
{
|
||||
$token = $request->cookie(AuthMiddleware::COOKIE_NAME);
|
||||
if (is_string($token) && $token !== '') {
|
||||
$this->logout->execute($token);
|
||||
}
|
||||
|
||||
$response = new JsonResponse(null, 204);
|
||||
|
||||
return $response->withCookie(Cookie::create(
|
||||
name: AuthMiddleware::COOKIE_NAME,
|
||||
value: '',
|
||||
expire: 1,
|
||||
path: '/',
|
||||
domain: null,
|
||||
secure: false,
|
||||
httpOnly: true,
|
||||
raw: false,
|
||||
sameSite: Cookie::SAMESITE_LAX,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{id: int, email: string}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue