add logout endpoint
This commit is contained in:
parent
89f448bd42
commit
22d9ad5136
3 changed files with 42 additions and 0 deletions
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Auth\UseCases\Logout;
|
||||||
|
|
||||||
|
use App\Auth\SessionRepository;
|
||||||
|
|
||||||
|
class Logout
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private SessionRepository $sessionRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function execute(string $token): void
|
||||||
|
{
|
||||||
|
$this->sessionRepository->deleteByToken($token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ namespace App\Http\Controllers;
|
||||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
||||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||||
use App\Auth\UseCases\CreateSession\CreateSession;
|
use App\Auth\UseCases\CreateSession\CreateSession;
|
||||||
|
use App\Auth\UseCases\Logout\Logout;
|
||||||
use App\Exceptions\BadRequestException;
|
use App\Exceptions\BadRequestException;
|
||||||
use App\Exceptions\UnauthorizedException;
|
use App\Exceptions\UnauthorizedException;
|
||||||
use App\Http\Middleware\AuthMiddleware;
|
use App\Http\Middleware\AuthMiddleware;
|
||||||
|
|
@ -19,6 +20,7 @@ class AuthController extends Controller
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private AuthenticateUser $authenticateUser,
|
private AuthenticateUser $authenticateUser,
|
||||||
private CreateSession $createSession,
|
private CreateSession $createSession,
|
||||||
|
private Logout $logout,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function login(Request $request): JsonResponse
|
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}
|
* @return array{id: int, email: string}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,5 @@ Route::post('/login', [AuthController::class, 'login']);
|
||||||
|
|
||||||
Route::middleware(AuthMiddleware::class)->group(function (): void {
|
Route::middleware(AuthMiddleware::class)->group(function (): void {
|
||||||
Route::get('/me', [AuthController::class, 'me']);
|
Route::get('/me', [AuthController::class, 'me']);
|
||||||
|
Route::post('/logout', [AuthController::class, 'logout']);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue