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?

  • 1
    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, 2014 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, 2014 at 14:09
  • 1
    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, 2014 at 14:11
  • I tried but had lot of UI issue so I thought it is better to go with this ... which is recommended according to you and why ?... sorry for asking so many question ..I am newbie in drupal
    – Hitesh
    Aug 7, 2014 at 14:13
  • 1
    Long story short: If you don't want to use Drupal's user and content management, it would be simpler just skipping Drupal altogether and coding from scratch. If you want / need Drupal, alter it's forms, but use them, and Drupal's built-in mechanisms. Recreating parts of Drupal to work with already existing ones will be a huge pain in posterior.
    – Mołot
    Aug 7, 2014 at 14:20

2 Answers 2


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;
$username = 'Username';
$pass = 'Password';

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

user_login_submit(array(), $uid); 
$load_user = user_load($uid);

if (in_array('role_name', $load_user->roles)) {
  // Add functionality that you want to do like if you want to redirect or anything else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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