Hi,
I am developing a module to fetch some user information from another system into drupal. I have overriden the form_validate of user_login hook and I can create a drupal user from this method. What do I need to do to log this user into drupal from the code?
function unilogin_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'user_login' || $form_id == 'user_login_block') {
debug($form, "userform content");
$form['#validate'][] = 'unilogin_user_form_validate';
}
}
function unilogin_user_form_validate($form, &$form_state) {
if(!unilogin_authenticate($form_state)) {
form_set_error('login', 'invalid login...');
}
}
function unilogin_authenticate($form_state) {
global $user;
if($user->uid) return FALSE;
$user = user_load_by_name($form_state['values']['name']);
debug($user, "loaded userinfo");
if($user != false) {
return TRUE;
}
$uid = $form_state['values']['name'];
$user_mail = $uid.'@example.com';
$account = new stdClass;
$account->is_new = TRUE;
$account->name = $uid;
$account->pass = user_hash_password($form_state['values']['pass']);
$account->mail = $user_mail;
$account->init = $user_mail;
$account->status = TRUE;
$account->roles = array(DRUPAL_AUTHENTICATED_RID => TRUE);
$account->timezone = variable_get('date_default_timezone', '');
debug($account);