2

I'm trying to autowire PasswordHasherInterface in the Fixtures class:

<?php
namespace App\DataFixtures;

use App\Model\User\Entity\User\Email;
use App\Model\User\Entity\User\Id;
use App\Model\User\Entity\User\Role;
use App\Model\User\Entity\User\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

class UserFixture extends Fixture
{
    private PasswordHasherInterface $hasher;

    public function __construct(PasswordHasherInterface $hasher)
    {
        $this->hasher = $hasher;
    }
    
    public function load(ObjectManager $manager): void
    {
        $hash = $this->hasher->hash("password");

        $user = User::signUpByEmail(
            Id::next(),
            new \DateTimeImmutable(),
            new Email("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

        $user->changeRole(Role::admin());

        $manager->persist($user);
        $manager->flush();
    }
}

But I got error:

In DefinitionErrorExceptionPass.php line 54: !!
!! Cannot autowire service "App\DataFixtures\UserFixture": argument "$hasher"
!! of method "__construct()" references interface "Symfony\Component\PasswordH
!! asher\PasswordHasherInterface" but no such service exists. Did you create a
!! class that implements this interface?
!!

My file services.yaml:

parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Model/User/Entity/'
            - '../src/Kernel.php'

How to hash plain password in Symfony 6.1? Why I get this error?

2
  • Could you post your file system structure? It appears you're missing a service/class that implements Symfony\Component\PasswordHasher\PasswordHasherInterface Commented Jul 5, 2022 at 20:04
  • 1
    This is basically a duplicate of stackoverflow.com/questions/62980930/… except that for S5 and later you use the PasswordHasherFactoryInterface instead of EncoderFactoryInterface. Commented Jul 5, 2022 at 20:29

1 Answer 1

3

There is no general PasswordHasher.

Either you:

  • generate a specific one using a factory: e.g. Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
  • or you use a dedicated password hasher class: e.g. Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface for users*.

Using a factory, your code would look like this: (untested)

//...
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;


class UserFixture extends Fixture
{
    private PasswordHasherFactoryInterface $passwordHasherFactory;

    public function __construct(PasswordHasherFactoryInterface $hasherFactory)
    {
      $this->passwordHasherFactory = $passwordHasherFactory;
    }
    
    public function load(ObjectManager $manager): void
    {
        $passwordHasher = $this->passwordHasherFactory->getPasswordHasher(User::class);
        $hash = $passwordHasher->hash("password");

        $user = User::signUpByEmail(
            Id::next(),
            new \DateTimeImmutable(),
            new Email("[email protected]"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

        $user->changeRole(Role::admin());

        $manager->persist($user);
        $manager->flush();
    }

Just to reiterate, the steps are:

  1. Install the package: composer require symfony/password-hasher
  2. Configure the hasher
  3. Load a hasher
    • Load the UserPasswordHasherInterface OR
    • Load the PasswordHasherFactoryInterface (see example) and get the PasswordHasher

*: An example of UserPasswordHasherInterface for a fixure is located here.

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.