How do I check the logged-in user username/password/role using a function to validate them? I am allowing users to register an account on my site. They get a specific role using user_save().

When they log in for the first time, I have to verify the login credentials. 

Is there a function to verify the login credentials?
Is there a function to get the user roles?
To check login credentials in Drupal 6&amp;7:

$uid = user_authenticate($username, $password);


In Drupal 8:

// Inject user.auth service..
$uid = $this-&gt;userAuth-&gt;authenticate($username, $password);
// or
$uid = \Drupal::service('user.auth')-&gt;authenticate($username, $password);


To check roles:

/**
 * Check User roles
 * @param $role
 * @param null $user
 * @return bool
 */
function user_has_role($roles, $user = NULL) {
  if ($user == NULL) {
    global $user;
  }
  if (!is_array($roles)) {
    $roles = array($roles);
  }
  if (count(array_intersect($roles, $user-&gt;roles))) {
    return TRUE;
  }
  return FALSE;
}