1

When a user tries to log into my drupal website, I want to check an external database to see if he is registered there. If he is registered in the external db, an account will be created on Drupal when he logs into Drupal for the first time. This is what I currently have to modify the login form to check the second database and create a drupal account for the user if necessary

function converge_form_alter(&$form, &$form_state, $form_id)  {
  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    $form['#submit'][] = 'converge_convergeAuthenticate';
  } 
}

However, this callback function only gets called on a successful login in Drupal. How can I get the system to run the callback function even if the users password and username are incorrect (and create local account if the incorrect password matches external database)?

I also tried the following code to get the custom function called first but it's not working.

 if ($form_id == 'user_login_block' || $form_id == 'user_login') {
    array_unshift($form['#submit'], 'converge_convergeAuthenticate');
  } 
2
function da_form_alter (&$form, $form_state, $form_id) {
   if ($form_id == 'user_login' || $form_id == 'user_login_block' ) {
     if (isset($form_state['post']['name'])) {
       $form['#validate'][] = da_login_validate($form, $form_state);
    }
  }
}

function da_login_validate($form, $form_state) {
  // Here you should check wheather the user is valid or not..
}

Better way to do this: you should read the source code of any module. For example, read the code of Facebook Connect which does the same thing.

  • Awesome. That worked perfectly. I don't see this in fbconnect – Matthew Hui Nov 9 '11 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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