Drupal - get user id from username or email without logging in
You can load the user with loadByProperties()
:
$users = \Drupal::entityTypeManager()->getStorage('user')
->loadByProperties(['mail' => $mail]);
$user = reset($users);
if ($user) {
$uid = $user->id();
$rids = $user->getRoles();
}
Drupal 8 now also has these specific functions:
user_load_by_name()
$user = user_load_by_name('some user');
$uid = $user->id();
https://api.drupal.org/api/drupal/core!modules!user!user.module/function/user_load_by_name/8.9.x
user_load_by_mail()
$user = user_load_by_mail('[email protected]');
$uid = $user->id();
https://api.drupal.org/api/drupal/core!modules!user!user.module/function/user_load_by_mail/8.9.x