Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/packages/lexik_jwt_authentication.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ lexik_jwt_authentication:
pass_phrase: '%env(JWT_PASSPHRASE)%'
# 1 month ttl
token_ttl: 2628288

token_extractors:
cookie:
enabled: true
name: !php/const App\Api\Controller\PlayerController::JWT_AUTH_COOKIE_NAME
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider:
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
Expand Down
12 changes: 10 additions & 2 deletions src/Api/Controller/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Infrastructure\Persistence\PersistenceAdapterInterface;
use App\Serializer\KillerSerializer;
use App\Validator\KillerValidator;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
Expand All @@ -37,6 +38,8 @@ class PlayerController extends AbstractController implements LoggerAwareInterfac
{
use LoggerAwareTrait;

public const JWT_AUTH_COOKIE_NAME = 'killer_auth';

public function __construct(
private readonly PlayerRepository $playerRepository,
private readonly RoomRepository $roomRepository,
Expand All @@ -45,6 +48,7 @@ public function __construct(
private readonly KillerSerializer $serializer,
private readonly KillerValidator $validator,
private readonly JWTTokenManagerInterface $tokenManager,
private readonly JWTCookieProvider $JWTCookieProvider,
private readonly RoomStatusTransitionUseCase $roomStatusTransitionUseCase,
) {
}
Expand All @@ -70,15 +74,19 @@ public function createPlayer(Request $request): JsonResponse
$this->playerRepository->store($player);
$this->persistenceAdapter->flush();

$player->setToken($this->tokenManager->create($player));
$token = $this->tokenManager->create($player);
$this->logger->info('Token created for player {user_id}', ['user_id' => $player->getId()]);

return $this->json(
$response = $this->json(
$player,
Response::HTTP_CREATED,
['Location' => sprintf('/player/%s', $player->getUserIdentifier())],
[AbstractNormalizer::GROUPS => 'create-player'],
);

$response->headers->setCookie($this->JWTCookieProvider->createCookie($token, self::JWT_AUTH_COOKIE_NAME));

return $response;
}

#[Route('/me', name: 'me', methods: [Request::METHOD_GET])]
Expand Down