This question already has an answer here:

I am allowing user to register in my site. while registering, user is assigned with a specific role using user_save

while logging in as registered user, I have to check if login credential are correct and username/password matches with the saved username/password in Drupal db.

I have following fields

Email : test@gmail.com
password : *****


How do I use Drupal function to check from the Drupal database , if the user has entered correct username/password, I also have to check the role of the user so that I can restrict user from seeing certain content which only admin is allowed to see

marked as duplicate by Mołot, Clive Aug 7 '14 at 13:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I have edited the question title ... I think now it is not duplicate – Hitesh Aug 7 '14 at 14:01
  • drupal.stackexchange.com/questions/15390/… .... this question is only answering .. if it has a role ...it does not check other username / password – Hitesh Aug 7 '14 at 14:02
  • But Drupal already takes care about checking username and password on login. And it's permission system already takes care about restricting user from seeing certain content which only admin is allowed to see. It looks like you are reinventing the wheel now - why would you? What exactly are you trying to do? – Mołot Aug 7 '14 at 14:04
  • I am providing the registration to public user ... I will be assigning roles to each user when they are registering. Now after registration when they are logging in [not using Drupal back end but in my login html form] as in normal Login I need to check un/pwd/role ... each role has different view for the website – Hitesh Aug 7 '14 at 14:09
  • But why are you avoiding Drupal's login form? You can alter it with hooks and still use built-in, proven and tested routines. – Mołot Aug 7 '14 at 14:11

To check login credentials in Drupal 6&7:

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

In Drupal 8:

// Inject user.auth service..
$uid = $this->userAuth->authenticate($username, $password);
// or
$uid = \Drupal::service('user.auth')->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->roles))) {
    return TRUE;
  return FALSE;
$userRecord = user_authenticate($username, $pass);
$uid= $userRecord; 
user_login_submit(array(), $uid); 
    // Add functionality that you want to do like if you want to redirect or anything else

Not the answer you're looking for? Browse other questions tagged or ask your own question.