Home  >  Q&A  >  body text

Unable to autowire PasswordHasherInterface

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

<?php
namespace AppDataFixtures;

use AppModelUserEntityUserEmail;
use AppModelUserEntityUserId;
use AppModelUserEntityUserRole;
use AppModelUserEntityUserUser;
use DoctrineBundleFixturesBundleFixture;
use DoctrinePersistenceObjectManager;
use SymfonyComponentPasswordHasherPasswordHasherInterface;

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("admin@app.test"),
            $hash,
            "token"
        );

        $user->confirmSignUp();

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

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

But I got the error:

In DefinitionErrorExceptionPass.php line 54: ! !

!!Unable to autowire service 'AppDataFixturesUserFixture': parameter '$hasher'

!!Method "__construct()" refers to interface "SymfonyComponentPasswordH

!!asherPasswordHasherInterface" but no such service exists. Did you create

!!The 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 passwords in Symfony 6.1? Why am I getting this error?

P粉154228483P粉154228483285 days ago445

reply all(1)I'll reply

  • P粉787820396

    P粉7878203962023-12-14 09:25:15

    There is no universal PasswordHasher.

    you:

    • Use a factory to generate a specific one: for example Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface
    • Or you use a dedicated password hasher class: e.g. Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface for user*.

    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("admin@app.test"),
                $hash,
                "token"
            );
    
            $user->confirmSignUp();
    
            $user->changeRole(Role::admin());
    
            $manager->persist($user);
            $manager->flush();
        }

    To reiterate, the steps are:

    1. Install the software package: composer require symfony/password-hasher
    2. Configure hasher
    3. Load hasher
      • LoadingUserPasswordHasherInterface or
      • Load PasswordHasherFactoryInterface (see Example) and get PasswordHasher

    *: The UserPasswordHasherInterface example for the fix is ​​located here.

    reply
    0
  • Cancelreply