Firewall
How to register and authenticate my users?
Security Bundle
composer require symfony/security-bundle<?php
namespace App\Security;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class UserProvider implements UserProviderInterface
{
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @throws UsernameNotFoundException if the user is not found
*/
public function loadUserByUsername($username): UserInterface
{
$user = $this->userRepository->find($username);
if (null !== $user) {
return $user;
}
throw new UsernameNotFoundException(sprintf('User with name "%s" does not exist', $username));
}
public function refreshUser(UserInterface $user): UserInterface
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class): bool
{
return User::class === $class;
}
}Firewall Configuration
Logout
User Authentication
Credential Request Options
Assertion Response
Request Profile
User Registration
Credential Creation Options
Attestation Response
Creation Profile
Authentication Attributes
Options Storage
Response Handlers
Request Options Handler
Creation Options Handler
Authentication Success Handler
Authentication Failure Handler
Last updated
Was this helpful?