@@ -226,6 +226,57 @@ After configuring the correct algorithm, you can use the
226226 throw new \Exception('Bad credentials, cannot delete this user.');
227227 }
228228
229+ Injecting a Specific Password Hasher
230+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231+
232+ In some cases, you might define a password hasher in your configuration that is
233+ not linked to a user entity but is instead identified by a unique key.
234+ For example, you might have a separate hasher for things like password recovery
235+ codes.
236+
237+ With the following configuration:
238+
239+ .. code-block :: yaml
240+
241+ # config/packages/security.yaml
242+ security :
243+ password_hashers :
244+ recovery_code : ' auto'
245+
246+ It is possible to inject the recovery_code password hasher into any service.
247+ To do this, you can't rely on standard autowiring, as Symfony wouldn't know
248+ which specific hasher to provide.
249+
250+ Instead, you can use the ``#[Target] `` attribute to request the hasher by its
251+ configuration key::
252+
253+ // src/Controller/HomepageController.php
254+ namespace App\Controller;
255+
256+ use Symfony\Component\DependencyInjection\Attribute\Target;
257+ use Symfony\Component\PasswordHasher\PasswordHasherInterface;
258+
259+ class HomepageController extends AbstractController
260+ {
261+ public function __construct(
262+ #[Target('recovery_code')]
263+ private readonly PasswordHasherInterface $passwordHasher,
264+ ) {
265+ }
266+
267+ #[Route('/')]
268+ public function index(): Response
269+ {
270+ $plaintextToken = 'some-secret-token';
271+
272+ // Note: use hash(), not hashPassword(), as we are not using a UserInterface object
273+ $hashedToken = $this->passwordHasher->hash($plaintextToken);
274+ }
275+ }
276+
277+ When injecting a specific hasher by its name, you should type-hint the generic
278+ :class: `Symfony\\ Component\\ PasswordHasher\\ PasswordHasherInterface `.
279+
229280Reset Password
230281--------------
231282
0 commit comments