// You can check if the user is authenticathed or not.
$is_authenticated = \Drupal::currentUser()->isAuthenticated();
$is_anonymous = \Drupal::currentUser()->isAnonymous();
// Or by doing:
$current_user = \Drupal::currentUser();
$current_user->isAnonymous(); // true or false
$current_user->isAuthenticated(); // true or false
// Get a timestamp of last login for the user.
$current_user->getLastAccessedTime();
// Get the current user id, only if is authenticated.
$uid = \Drupal::currentUser()->id();
// Load a specific user by id.
// Is a user entity.
$user = \Drupal\user\Entity\User::load($uid);
// Load the current user.
// Remember: is a user object not a user entity.
$current_user = \Drupal::currentUser();
// Check existing fields and get specific values from user's fields.
$existing_field = $user->hasField('mail');
$user_display_name = $current_user->getDisplayName();
$user_account_name = $current_user->getAccountName();
$user_name = $current_user->getUsername();
// Different ways for getting data from fields.
$user_mail = $user->get('mail')->getValue()[0]['value'];
$user_mail_copy = $user->mail->value;
$user_mail_another_copy = $current_user->getEmail();
// Get all the fields available in the user entity type.
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user');
$extra_fields = \Drupal::service('entity_field.manager')->getExtraFields('user', 'user');
$base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('user', 'user');
$existing_field = array_key_exists('association', $fields);
// Get an array of roles from a user.
$array_roles = $user->getRoles();
// Check if the user has a specific role.
$existing_role = in_array('administrator', $array_roles);