[Drupal] How to redirect users to different URLs on a Drupal site after Facebook login?

| | 1 min read

In one of our Drupal projects, we implemented a feature where, users can login to the site through Facebook login(fconnect button). Facebook login(Fconnect) button was implemented using fbconnect module. Client requirement was to change the landing page for each user according to their role after login through facebook.

To achieve this requirement we need to alter 'fbconnect_autoconnect_form' from our custom module i.e. When the user clicks facebook login button, facebook login form is loaded and they can login using thier facebook account. If the login is successful and facebook session is detected, then 'fbconnect_autoconnect_form' is submitted by javascript function 'facebook_onlogin_ready()'. We can alter this form from our custom module to call new submit handler.

Programatic implementation of this requirement

function modulename_form_alter(&$form, $form_state, $form_id) {
    ...
    case 'fbconnect_autoconnect_form':
      // set form action to null
      $form['#action'] = ' '; 
      // Add new submit handler.
      $form['#submit'][] = 'modulename_fbconnect_autoconnect_form'; 
} 
function modulename_fbconnect_autoconnect_form($form, &$form_state) {
  global $user; 
  if($user->uid > 0) { 
    // Check the user role and redirect user to corresponding page.  
    if (((in_array('user role', $user->roles))) { 
      $url = 'Required URL';
      drupal_goto($url);
    }
    else {
      $url = 'Required URL2';
      drupal_goto($url);
    }
  }
}