dimkinthepro/jwt-auth-bundle

This bundle provides JWT authentication

1.0.0 2023-09-24 18:19 UTC

This package is auto-updated.

Last update: 2024-04-24 19:36:14 UTC


README

1. Installation:

composer require dimkinthepro/jwt-auth-bundle

2. Check bundles config:

# config/bundles.php

return [
#...
    Dimkinthepro\JwtAuth\DimkintheproJwtAuthBundle::class => ['all' => true],
];

3. Create bundle configuration:

# config/packages/dimkinthepro_jwt_auth.yaml
dimkinthepro_jwt_auth:
    public_key_path: '%kernel.project_dir%/var/dimkinthepro/jwt-auth-bundle/public.pem'
    private_key_path: '%kernel.project_dir%/var/dimkinthepro/jwt-auth-bundle/private.pem'
    passphrase: 'SomeRandomPassPhrase'
    token_ttl: 36000 # 10 hour
    algorithm: 'RS512'
    refresh_token_ttl: 2592000 # 1 month
    refresh_token_length: 128 # max=255

4. Add security configuration

# config/packages/security.yaml

security:
  #...
  main:
      lazy: true
      auth_jwt: ~
      pattern: ^/api/
      stateless: true
      provider: your_app_user_provider
      json_login:
          check_path: /api/user/login
          username_path: email
          success_handler: Dimkinthepro\JwtAuth\Infrastructure\Security\SuccessAuthenticationHandler
          failure_handler: Dimkinthepro\JwtAuth\Infrastructure\Security\FailAuthenticationHandler

5. Add doctrine configuration

# config/packages/doctrine.yaml
doctrine:
    #...
    orm:
        #...
        mappings:
            #...
            DimkintheproJwtAuthBundle:
                is_bundle: true
                type: xml
                prefix: Dimkinthepro\JwtAuth\Domain\Entity

After that you can generate migrations:

php bin/console doctrine:migrations:diff

php bin/console doctrine:migrations:migrate

6. Generate key pair:

bin/console dimkinthepro:jwt-auth:generate-key-pair