You are here

function twitter_signin_oauth_callback_submit in Twitter 6.5

Same name and namespace in other branches
  1. 6.3 twitter_signin/twitter_signin.module \twitter_signin_oauth_callback_submit()
  2. 6.4 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
Implements hook_form_alter().

File

twitter_signin/twitter_signin.module, line 163
Hook implementations for twitter_signin submodule.

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'];

  // Attempt to login as the user.
  $account = user_external_load($response['user_id']);

  // The login was successful.
  if (isset($account->uid)) {

    // Finish the login process.
    user_external_login($account, $response);
    return TRUE;
  }
  else {

    // See if the user exists on the site.
    $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.
    if (!empty($uid)) {
      $account = user_load($uid);
      $edit['authname_twitter'] = $response['user_id'];
      $edit['name'] = $account->name;
      user_save($account, $edit);
      $user = $account;
      $form_state['twitter_oauth']['account'] = $account;
      $success = TRUE;
    }
    elseif (variable_get('twitter_signin_register', 0)) {

      // Check for a nickname collision.
      $account = user_load(array(
        'name' => $response['screen_name'],
      ));
      if (empty($account->uid)) {
        $password = user_password();
        $edit = array(
          'name' => $response['screen_name'],
          'pass' => $password,
          'init' => $response['screen_name'],
          'status' => 1,
          'authname_twitter' => $response['user_id'],
          'access' => time(),
        );

        // Try saving the new user account.
        try {
          $account = user_save('', $edit);
        } catch (Exception $e) {
          watchdog('oauth_login', "Exception saving new account: " . $e
            ->getMessage(), '', WATCHDOG_WARNING);
          return FALSE;
        }

        // Reload the user's account object.
        $account = user_load($account->uid);

        // Connect the Drupal user account and the Twitter account together.
        $form_state['twitter_oauth']['account'] = $account;
        $args = array(
          '!password' => $password,
          '!link' => l('set your account settings', 'user/' . $account->uid . '/edit'),
        );
        if (variable_get('twitter_signin_show_password', TRUE)) {
          drupal_set_message(t('You have been automatically registered with the password !password. Copy it to !link.', $args));
        }
        $success = TRUE;
      }
      else {
        $args = array(
          '%name' => $response['screen_name'],
          '@login' => url('user/login'),
        );
        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.', $args), 'warning');
      }
    }
    else {
      drupal_set_message(t('Please log in or register to relate your Twitter account with a user.'));
    }
    if ($success) {

      // Log the user into their new account.
      $user = $account;

      // Trigger hook_user_login and other post-login cleanup.
      user_authenticate_finalize($edit);
      return TRUE;
    }
  }
  if (!$success) {
    $_SESSION['twitter']['values'] = $response;
    drupal_goto('user/login');
  }
}