PHP code example of k3progetti / jwt-bundle

1. Go to this page and download the library: Download k3progetti/jwt-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

k3progetti / jwt-bundle example snippets


return [
    // ...
    K3Progetti\JwtBundle\JwtBundle::class => ['all' => true],
];

use K3Progetti\JwtBundle\Security\JwtUserInterface;

class User implements JwtUserInterface
{
    public function getId(): mixed { ... }
    public function getUsername(): string { ... }
    public function getName(): string { ... }
    public function getSurname(): string { ... }
    public function isActive(): bool { ... }

    // Campi richiesti per il 2FA
    public function isTwoFactorAuth(): bool { ... }
    public function getTwoFactorAuthCode(): ?string { ... }
    public function setTwoFactorAuthCode(?string $code): static { ... }
    public function setTwoFactorAuthCodeExpired(\DateTimeInterface $dt): static { ... }
}

use K3Progetti\JwtBundle\Repository\JwtUserRepositoryInterface;
use K3Progetti\JwtBundle\Security\JwtUserInterface;

class UserRepository implements JwtUserRepositoryInterface
{
    public function findOneBy(array $criteria, ?array $orderBy = null): ?JwtUserInterface { ... }
    public function save(JwtUserInterface $user): void { ... }
}

use K3Progetti\JwtBundle\Mailer\TwoFactorMailerInterface;
use K3Progetti\JwtBundle\Security\JwtUserInterface;

class PostmarkService implements TwoFactorMailerInterface
{
    public function sendTwoFactorCode(JwtUserInterface $user, string $code): void { ... }
}

use K3Progetti\JwtBundle\Service\JwtPayloadInterface;
use K3Progetti\JwtBundle\Security\JwtUserInterface;

class MyPayloadModifier implements JwtPayloadInterface
{
    // Aggiunge dati prima della costruzione del payload base
    public function onBeforePayload(JwtUserInterface $user): array { ... }

    // Modifica il payload dopo la costruzione base
    public function onAfterPayload(array $payload, JwtUserInterface $user): array { ... }

    // Sovrascrive completamente il payload (restituire null per non sovrascrivere)
    public function overridePayload(JwtUserInterface $user): ?array { ... }
}
bash
php composer.phar install --ignore-platform-req=ext-redis
bash
php bin/console make:migration
php bin/console doctrine:migrations:migrate