You are here

function twitter_signin_oauth_callback_submit in Twitter 6.4

Same name and namespace in other branches
  1. 6.5 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  2. 6.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  3. 7.6 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  4. 7.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  5. 7.4 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  6. 7.5 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()

Form submit for the OAuth callback. Here we add in sign-in specific handling

1 string reference to 'twitter_signin_oauth_callback_submit'
twitter_signin_form_alter in twitter_signin/twitter_signin.module
Implementation of hook_form_alter()

File

twitter_signin/twitter_signin.module, line 119
Hook implementations for twitter_signin module.

Code

function twitter_signin_oauth_callback_submit(&$form, &$form_state) {
  global $user;
  $success = FALSE;
  $key = variable_get('twitter_consumer_key', '');
  $secret = variable_get('twitter_consumer_secret', '');
  $response = $form_state['twitter_oauth']['response'];
  $account = user_external_load($response['user_id']);
  if (isset($account->uid)) {
    user_external_login($account, $response);
    $form_state['twitter_oauth']['account'] = $account;
    $success = TRUE;
  }
  elseif ($uid = db_result(db_query("SELECT uid FROM {twitter_account} WHERE twitter_uid= %d", $response['user_id']))) {

    // We have an existing twitter account - set it up for login
    $account = user_load($uid);
    $edit["authname_twitter"] = $response['user_id'];
    user_save($account, $edit);
    $user = $account;
    $form_state['twitter_oauth']['account'] = $account;
    $success = TRUE;
  }
  else {

    // No existing user account, let's see if we can register.
    if (variable_get('twitter_signin_register', 0)) {

      // Check for a nickname collision
      $account = user_load(array(
        'name' => $response['screen_name'],
      ));
      if (!$account->uid) {
        $edit = array(
          'name' => $response['screen_name'],
          'pass' => user_password(),
          'init' => $response['screen_name'],
          'status' => 1,
          "authname_twitter" => $response['user_id'],
          'access' => time(),
        );
        $account = user_save('', $edit);
        $user = $account;
        $success = TRUE;
      }
      else {
        drupal_set_message(t('The nickname %name is already in use on this site, please register below with a new nick name, or @login to continue.', array(
          '%name' => $response['screen_name'],
          '@login' => url('user/login'),
        )), 'warning');
      }
    }
    else {
      drupal_set_message(t('Please complete the following registration form to create your new account on %site', array(
        '%site' => variable_get('site_name', ''),
      )));
    }
  }
  if (!$success) {
    $_SESSION['twitter']['values'] = $response;
    drupal_goto('user/register');
  }
}