Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am looking for the API that will let me login a user by passing it the username and password. Does anyone have experience with this?

To clarify, I am trying to make an AJAX login box that appears as a popup on the home page, and do not refresh the page in case of wrong credentials, but only if the log in is correct. So here is what i have done so far:

Update

I now load the login form on my homepage, then on submission I launch an AJAX request that sends credential to this script:

function user_login_submit_try() {
  global $user;  

  $uid = user_authenticate($_POST['name'],$_POST['pass']);    
  $arr = array ('name'=>$_POST['name'],'pass'=>$_POST['pass']);
  if ($uid){
    $user = user_load($uid);
    user_login_finalize($arr);
  }

  echo drupal_json_encode($uid); 
  exit;
}; 

So far it works, but my worries are (as mentioned by googletorp) security issues; it seems that none of the API I used in this script sanitized the data in anyway.

Would anyone see a better way to do so?

share|improve this question
For which reason would you need to log in a user from code using username, and password? – kiamlaluno Jun 25 '11 at 12:33
@ kiamlaluno Because i need to use Ajax on the login form. In my precedent questions: drupal.stackexchange.com/questions/5780/… drupal.stackexchange.com/questions/5781/… I couldn't solve any of these two issues so i went for a custom ajax request. – silkAdmin Jun 27 '11 at 10:13

3 Answers

up vote 10 down vote accepted

This might help someone:

function mymodule_user_login_credentials($username, $password)
{
    if(user_authenticate($username, $password))
    {
      $user_obj = user_load_by_name($username);
      $form_state = array();
      $form_state['uid'] = $user_obj->uid;      
      user_login_submit(array(), $form_state);
      return true;
    }
    else
    {
        return false;
    }
}
share|improve this answer

There is not a simple API function for that.

You can do what Drupal does:

  1. Test the password by querying the database see:

    user_login_name_validate
    user_login_authenticate_validate
    user_login_final_validate
    
  2. Overwrite the global $user.

    global $user;
    $user = user_load($uid);
    
  3. Handle sessions

    user_login_finalize($form_state);
    

For step two and 3 see user_login_submit

All these functions are based on being called from a form. The normal flow is that the validate handlers test the users input and returns the uid of the user if validation passes. The submit handler then handles the actual login of the user and calling user hook.

share|improve this answer
thanks googletorp, i ll try that out, although this let me wonder on how this functions work together.. How does one know that i did invoke the precedent ? – silkAdmin Jun 25 '11 at 9:16
oh and if i do not have the From_state variable availlable but just username and password, can i rebuild it like : $form_state['value']['name'] = $_POST['name'] and so on.. – silkAdmin Jun 25 '11 at 9:18
@silk You should always use the Drupal FAPI which gives you the $form_state variable. There are a lot of nice security checks that you would otherwise loose and possible open your site for attacks. When it comes to logging in a user, you don't want to loose that security. – googletorp Jun 25 '11 at 9:48
Please check my updated question, i'd really like to know if my inputs get cleaned up.. Thanks – silkAdmin Jun 25 '11 at 10:12
Sorry it seems that the edit didn't work the first time, now it's updated. – silkAdmin Jun 27 '11 at 13:26

If you need to get the user object for the user account for which you know username and password, then you can use the following code:

function mymodule_get_user(name, $password) {
  $account = FALSE;

  if ($uid = user_authenticate($name, $password)) {
    $account = user_load($uid);
  }

  return $account;
}

$account will be FALSE if the user object cannot be found or loaded.

You should use this code when, for example, your module gets the username and password as input from a user, it verifies if they are correct, and then it does something with the user object.
If you are writing a module that needs to impersonate a user to write a comment, or doing something else that needs to result as done by a user, which is what the Project issue tracking module does when closing an issue report that has been in the fixed status for two weeks (in that case, it adds the comment "Automatically closed -- issue fixed for 2 weeks with no activity" which results written by the "System Message" user), then you don't need username and password. You just load the user object for which you know the user ID.

As far as I know, there aren't any security issues with the code I reported.
You could want to limit the number of failed logins; or to temporary block the IP which tries to log in without success, or tries different logins in short time.

share|improve this answer
thank you kiamlaluno , it kind of what i did, but i am now worrying about security issues, please check out my updated question – silkAdmin Jun 27 '11 at 13:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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