Skip to content

Commit 74adccb

Browse files
committed
minor #14849 [Security] expand code example to clarify usage of the UserRepository (xabbuh)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] expand code example to clarify usage of the UserRepository fixes #14833 Commits ------- b9fe645 expand code example to clarify usage of the UserRepository
2 parents 29a9697 + b9fe645 commit 74adccb

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

security/experimental_authenticators.rst

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,13 +457,33 @@ using :ref:`the user provider <security-user-providers>`::
457457
and must return a ``UserInterface`` object (otherwise a
458458
``UsernameNotFoundException`` is thrown)::
459459

460+
// src/Security/CustomAuthenticator.php
461+
namespace App\Security;
462+
463+
use App\Repository\UserRepository;
460464
// ...
461-
$passport = new Passport(
462-
new UserBadge($email, function ($userIdentifier) {
463-
return $this->userRepository->findOneBy(['email' => $userIdentifier]);
464-
}),
465-
$credentials
466-
);
465+
466+
class CustomAuthenticator extends AbstractAuthenticator
467+
{
468+
private $userRepository;
469+
470+
public function __construct(UserRepository $userRepository)
471+
{
472+
$this->userRepository = $userRepository;
473+
}
474+
475+
public function authenticate(Request $request): PassportInterface
476+
{
477+
// ...
478+
479+
return new Passport(
480+
new UserBadge($email, function ($userIdentifier) {
481+
return $this->userRepository->findOneBy(['email' => $userIdentifier]);
482+
}),
483+
$credentials
484+
);
485+
}
486+
}
467487

468488
The following credential classes are supported by default:
469489

0 commit comments

Comments
 (0)