Alexandre Corvino
Alexandre Corvino

Reputation: 329

Get a UserPasswordEncoder Instance in Symfony 4

I did a user security system without FOSUserBundle and now I'm trying to place the oldUser entity in the newUser entity that implement a user interface.

For do that i have to encode the password like in the documentation inside the entity

But I can't get an instance of UserPasswordEncoder

When I try to get it from the autowiring symfony4 tell me

Controller "App\Controller\SecurityController::login()" requires that you provide a value for the "$passwordEncoder" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

So I tried to do a new UserPasswordEncoder() (just for try)

Type error: Too few arguments to function Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::__construct(), 0 passed in /home/connexio/dev/project/src/Controller/SecurityController.php on line 87 and exactly 1 expected

I also tried bin/console debug:container --show-private | grep -i Password

Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface alias for "security.user_password_encoder.generic"
security.authentication.listener.form
Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener
security.authentication.listener.form.main Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener
security.authentication.listener.json Symfony\Component\Security\Http\Firewall\UsernamePasswordJsonAuthenticationListener
security.command.user_password_encoder Symfony\Bundle\SecurityBundle\Command\UserPasswordEncoderCommand
security.password_encoder alias for "security.user_password_encoder.generic"
security.user_password_encoder.generic Symfony\Component\Security\Core\Encoder\UserPasswordEncoder
security.validator.user_password
Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator

And this is where I want to invoke the instance

/**
 * @Route("/login", name="security_login")
 */
public function login(AuthenticationUtils $helper, UserPasswordEncoder $passwordEncoder): Response
{
    $this->addUsersToUser($passwordEncoder);
    return $this->render('login.html.twig', [....

Someone know how to get an instance of UserPasswordEncoder ?

Thank's !

Upvotes: 0

Views: 3470

Answers (1)

Alexandre Corvino
Alexandre Corvino

Reputation: 329

Use UserPasswordEncoderInterface instead of UserPasswordEncoder worked and encode as well....

Upvotes: 1

Related Questions