/symfony-skills:jwt-authenticationfrom symfony-skills
Implements stateless JWT authentication in Symfony with LexikJWTAuthenticationBundle — login endpoint, token issuance, refresh tokens, custom claims, and JWT firewall configuration.
Ships in symfony-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
composer require lexik/jwt-authentication-bundle
php bin/console lexik:jwt:generate-keypair # writes config/jwt/private.pem + public.pem
# config/packages/lexik_jwt_authentication.yaml
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: 900 # 15 min access token — keep it SHORT
The private key and passphrase are secrets — store them in the Symfony secrets vault, never commit them.
# config/packages/security.yaml
security:
firewalls:
login:
pattern: ^/api/login$
stateless: true
json_login:
check_path: /api/login # POST { "email": "...", "password": "..." }
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
jwt: ~ # validates the Bearer token
access_control:
- { path: ^/api/login, roles: PUBLIC_ACCESS }
- { path: ^/api/token/refresh, roles: PUBLIC_ACCESS }
- { path: ^/api, roles: ROLE_USER }
json_login + the success handler issues the token; the jwt authenticator validates it on every subsequent request. Both firewalls are stateless — no session.
POST /api/login with credentials returns:
{ "token": "eyJ0eXAiOiJKV1Qi..." }
The client sends it back as Authorization: Bearer <token> on protected requests.
Access tokens must be short-lived (5–15 min) because they can't be revoked before expiry. For "stay logged in", pair them with a refresh token (gesdinet/jwt-refresh-token-bundle):
# config/packages/gesdinet_jwt_refresh_token.yaml
gesdinet_jwt_refresh_token:
refresh_token_class: App\Entity\RefreshToken
ttl: 2592000 # 30 days
single_use: true # rotate on each refresh
# routes
api_refresh_token:
path: /api/token/refresh
controller: gesdinet.jwtrefreshtoken::refresh
Refresh tokens are stored server-side, so they can be revoked (logout, password change, compromise). single_use: true rotates them, limiting replay.
Add claims (e.g. roles, tenant) at issuance via an event listener; read them later without a DB hit.
#[AsEventListener(event: Events::JWT_CREATED)]
final class JwtCreatedListener
{
public function __invoke(JWTCreatedEvent $event): void
{
$user = $event->getUser();
if ($user instanceof User) {
$payload = $event->getData();
$payload['uid'] = $user->getId()->toRfc4122();
$payload['tenant'] = $user->getTenantId();
$event->setData($payload);
}
}
}
Keep the payload small and never put secrets or sensitive data in a JWT — it's base64, not encrypted; anyone holding it can read it.
#[Route('/api/me', methods: ['GET'])]
public function me(#[CurrentUser] User $user): JsonResponse
{
return $this->json(UserResponse::fromEntity($user));
}
exp/iat (Lexik does this) — don't disable signature/expiry checks.localStorage for browser SPAs if you can use an HttpOnly cookie (XSS risk).private.pem / the passphrase — store keys/passphrase in the secrets vault.stateless: true.#[CurrentUser] / the token.json_login (Symfony 7).What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
jwt, symfony-authentication, lexik-jwt, bearer-token, stateless-auth
npx claudepluginhub fatonh/symfony-skills --plugin symfony-skills17 skills in this plugin
/api-platform-resourcesUse when exposing data through API Platform 4 — defining #[ApiResource], operations, DTO resources, state providers/processors, serialization groups, filters, and pagination. Use when the task mentions API Platform, ApiResource, state processor, or exposing an entity as a REST/GraphQL endpoint.
/bundle-organizationUse when structuring a Symfony project's src/ directory, organizing code into modules, configuring services, or deciding where a class belongs. Covers the modular monolith layout, service configuration, and config/ conventions for Symfony 7 (Flex, no app bundle).
/console-commandsUse when creating or refactoring Symfony console commands — never extends Command. On Symfony 8.1+ put #[AsCommand] on a method of a service class (even for a single command); on 7.x use the invokable attribute class. Also covers typed CLI inputs via value resolvers and #[MapInput] DTOs. Use when the task mentions console command, #[AsCommand], bin/console, #[Argument], #[Option], or a CLI task.
Other plugins' skills for the same job