danskernesdigitalebibliotek/agency-auth-bundle
php -v and composer require php:^8.0).composer.json for symfony/* packages; update via composer update symfony/*).Symfony\Component\Security\Core\User\UserInterface).UserInterface (or extend Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface for password auth).use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
class User extends Authenticatable implements PasswordAuthenticatedUserInterface
{
// ...
}
Auth facade or Symfony’s Security component:
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
// Example authenticator (if using Symfony’s security system)
public function authenticate(Request $request): Passport
{
return new Passport(new UserBadge($request->email));
}
Symfony Security Integration:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
class ApiKeyAuthenticator extends AbstractAuthenticator
{
public function supports(Request $request): ?bool { /* ... */ }
public function authenticate(Request $request): Passport { /* ... */ }
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewall): ?Response { /* ... */ }
}
config/packages/security.yaml:
firewalls:
main:
authenticators: [App\Auth\ApiKeyAuthenticator]
Password Handling:
UserPasswordHasherInterface for hashing/verifying passwords:
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
$hasher = app(UserPasswordHasherInterface::class);
$hashedPassword = $hasher->hashPassword($user, 'plainPassword');
Laravel Compatibility:
Auth facade, ensure it’s configured to work with Symfony’s security system (e.g., via Symfony\Bridge\Laravel\HttpFoundation\LaravelRequest).use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class AuthMiddleware
{
public function __construct(private TokenStorageInterface $tokenStorage) {}
public function handle(Request $request, Closure $next) { /* ... */ }
}
Migration Workflow:
composer update).UserInterface.Auth::attempt()) with Symfony’s AuthenticationUtils or custom authenticators.Breaking Changes:
Auth guard system directly. Custom guards must now implement Symfony’s AuthenticatorInterface or GuardInterface.UserInterface will cause runtime errors when Symfony’s security system attempts to load the user.Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken → use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge instead).Debugging Tips:
APP_DEBUG=true) and check the security.event.authentication_success/authentication_failure events.dd($this->tokenStorage->getToken()) to inspect the current token in middleware.$authenticator->authenticate() in custom authenticators.onAuthenticationFailure properly (returns a Response or throws an exception).Extension Points:
Symfony\Component\Security\Core\User\UserProviderInterface for non-database users (e.g., OAuth).security.interactive_login):
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
public function onInteractiveLogin(InteractiveLoginEvent $event) {
// Log login or update user metadata
}
PasswordResetTokenManagerInterface for token-based resets.Performance:
UserProvider uses efficient queries (e.g., where('email', $email)->first()).Configuration Quirks:
/admin) before generic ones (^/).Symfony\Component\Security\Core\Authentication\AnonymousToken for unauthenticated requests in custom logic.How can I help you explore Laravel packages today?