Skip to content

Commit

Permalink
Allow to complete password_credential flow with either username or email
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Apr 29, 2024
1 parent cd137d2 commit 965dd8c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EventListener/UserResolveListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public function __construct(UserRepository $userRepository, PasswordValidatorSer

public function onUserResolve(UserResolveEvent $event): void
{
$user = $this->userRepository->findOneBy(['email' => $event->getUsername()]);
$user = $this->userRepository->findOneByIdentifier($event->getUsername());

if ($user && $this->passwordValidator->isPasswordValid($user->getPassword(), $event->getPassword())) {
$event->setUser($user);
}
}
}
}
14 changes: 14 additions & 0 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ public function findById(string $userId): ?User
{
return $this->findOneBy(["id" => $userId]);
}

/**
* Find an User by any of it's unique properties
* @param string $identifier A user id or email
*/
public function findOneByIdentifier(string $identifier): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.id = :val')
->orWhere('u.email = :val')
->setParameter('val', $identifier)
->getQuery()
->getOneOrNullResult();
}
}

0 comments on commit 965dd8c

Please sign in to comment.