Skip to content

[Security] expand code example to clarify usage of the UserRepository #14849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions security/experimental_authenticators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,33 @@ using :ref:`the user provider <security-user-providers>`::
and must return a ``UserInterface`` object (otherwise a
``UsernameNotFoundException`` is thrown)::

// src/Security/CustomAuthenticator.php
namespace App\Security;

use App\Repository\UserRepository;
// ...
$passport = new Passport(
new UserBadge($email, function ($userIdentifier) {
return $this->userRepository->findOneBy(['email' => $userIdentifier]);
}),
$credentials
);

class CustomAuthenticator extends AbstractAuthenticator
{
private $userRepository;

public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}

public function authenticate(Request $request): PassportInterface
{
// ...

return new Passport(
new UserBadge($email, function ($userIdentifier) {
return $this->userRepository->findOneBy(['email' => $userIdentifier]);
}),
$credentials
);
}
}

The following credential classes are supported by default:

Expand Down