Argument 3 passed to Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken::__construct() must be of the type array, null given,
called in /home/ja/Sites/lopus-accmaker/vendor/symfony/security-guard/AbstractGuardAuthenticator.php on line 35
Problem udało się rozwiązać, odpowiedź na dole.
Opis sytuacji:
Robie aplikacje, które przeprowadzać będzie różne akcje na bazie danych, którą dostarcza mi gra. Baza danych jest gotowa, konta użytkowników przechowuje baza danych o nazwie "Accounts" za pomocą narzedzie wykonałem sobie zrzut struktury bazy danych do Encji w symfony.
Wszystko wygląda cudownie do momentu autentykacji. Wszystko przechodzi, dodałem własny password encoder
class Sha1PasswordEncoder implements PasswordEncoderInterface
{
/**
* @inheritDoc
*/
public function encodePassword(string $raw, ?string $salt)
{
return sha1($raw);
}
/**
* @inheritDoc
*/
public function isPasswordValid(string $encoded, string $raw, ?string $salt)
{
return sha1($raw) == $encoded;
}
Korzystam z niego w autentykatorze, metodzie checkCredentials
public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid(
$this->passwordEncoder->encodePassword($credentials['password'], null),
$credentials['password'],
null
);
}
Return zwraca true. Po przejściu dalej zwraca błąd z góry tego posta.
Najpierw autentykator budowałem sam, jednakże po spotkaniu tego błędy spróbowałem wygenerować go automtycznie. Niestety błąd się powtarza.
Cały kod:
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private $entityManager;
private $urlGenerator;
private $csrfTokenManager;
/**
* @var Sha1PasswordEncoder
*/
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager, Sha1PasswordEncoder $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
}
public function supports(Request $request)
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
}
public function getCredentials(Request $request)
{
$credentials = [
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
];
$request->getSession()->set(
Security::LAST_USERNAME,
$credentials['email']
);
return $credentials;
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
if (!$this->csrfTokenManager->isTokenValid($token)) {
throw new InvalidCsrfTokenException();
}
$user = $this->entityManager->getRepository(Accounts::class)->findOneBy(['email' => $credentials['email']]);
if (!$user) {
// fail authentication with a custom error
throw new CustomUserMessageAuthenticationException('Email could not be found.');
}
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid(
$this->passwordEncoder->encodePassword($credentials['password'], null),
$credentials['password'],
null
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
return new RedirectResponse($targetPath);
}
// For example : return new RedirectResponse($this->urlGenerator->generate('some_route'));
throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
}
protected function getLoginUrl()
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}
ROZWIĄZANIE:
Ok problemem było brak pola roles w bazie oraz encji.