How do I programmatically log in a user? 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'=&gt;$_POST['name'],'pass'=&gt;$_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?
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-&gt;uid;      
      user_login_submit(array(), $form_state);
      return true;
    }
    else
    {
        return false;
    }
}


A better version based on the comment:

function mymodule_user_login_credentials($username, $password)
{
    if($uid = user_authenticate($username, $password))
    {
      user_login_submit(array(), array('uid' =&gt; $uid));
      return true;
    }
    else
    {
        return false;
    }
}