0

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 !

5
  • 1
    Type hint against Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface should work as long as you only have one encoder. Note the Interface suffix. Otherwise use: "bin/console debug:container --show-private | grep -i Password" to see the available encoders. Commented Apr 6, 2018 at 13:30
  • I edited, but what does it means ? I should be able to use it no ? Commented Apr 6, 2018 at 13:51
  • Is your SecurityController::login a request action method or just a method you are calling from somewhere else? I don't understand why you are not using the built in form_login support for logging in. I understand having to encode a password when registering a user but the SecirityBundle takes care of login chores. Commented Apr 6, 2018 at 14:16
  • It's a route call when i want to login , I just wanted to call my addUserInDatabase() from somewhere (at the moment I call it from SecurityController::login), so i did it from a route based method to get the parameter from the autowiring system. But I will edit for show you the short code if it can help ^^ Commented Apr 6, 2018 at 14:31
  • Sorry. Still don't understand why you are fooling around with the encoder inside of GET /login. But even what you showed is missing the Interface suffix. Might need to spend some time with the autowire docs to see why it is important. Commented Apr 6, 2018 at 14:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.