<?php
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
class TwitchAuthenticator extends AbstractAuthenticator
{
public function __construct(
private EntityManagerInterface $_em
)
{}
public function supports(Request $request): ?bool
{
return $request->getSession()->has('login_user');
}
public function authenticate(Request $request): Passport
{
$uuid = $request->getSession()->get('login_user');
$user = $this->_em->getRepository(User::class)->findOneBy(['uuid' => $uuid]);
if (!$user) {
throw new AuthenticationException('The user is not found.');
}
return new SelfValidatingPassport(
new UserBadge($user->getUuid()),
[
new RememberMeBadge(),
]);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
return null;
}
}