You are here

public function ApiController::facebookLogin in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 src/Controller/ApiController.php \Drupal\fb_instant_articles\Controller\ApiController::facebookLogin()

Handle Facebook login callback.

1 string reference to 'ApiController::facebookLogin'
fb_instant_articles.routing.yml in ./fb_instant_articles.routing.yml
fb_instant_articles.routing.yml

File

src/Controller/ApiController.php, line 48

Class

ApiController
Controller to handle Facebook login callback.

Namespace

Drupal\fb_instant_articles\Controller

Code

public function facebookLogin() {
  $config = $this->configFactory
    ->getEditable('fb_instant_articles.settings');
  $fb = new Facebook([
    'app_id' => $config
      ->get('app_id'),
    'app_secret' => $config
      ->get('app_secret'),
    'default_graph_version' => 'v2.5',
  ]);
  $helper = $fb
    ->getRedirectLoginHelper();

  // Grab the user access token based on callback response and report back an
  // error if we weren't able to get one.
  try {
    $access_token = $helper
      ->getAccessToken();
    if ($access_token == NULL) {
      $error_msg = $this
        ->t('We failed to authenticate your Facebook account with this module. Please try again.');
      $this
        ->messenger()
        ->addError($error_msg);
    }
    else {

      // Confirm that the person granted the necessary permissions before
      // proceeding.
      $permissions = $fb
        ->get('/me/permissions', $access_token)
        ->getGraphEdge();
      $rejected_permissions = [];
      foreach ($permissions as $permission) {
        if ($permission
          ->getField('status') != 'granted') {
          $rejected_permissions[] = $permission
            ->getField('permission');
        }
      }
      if (!empty($rejected_permissions)) {
        $error_msg = $this
          ->t('You did not grant the following required permissions in the Facebook authentication process: @permissions. Please try again.', [
          '@permissions' => implode(', ', $rejected_permissions),
        ]);
        $this
          ->messenger()
          ->addError($error_msg);
      }
      else {

        // Store this user access token to the database.
        $config
          ->set('access_token', $access_token
          ->getValue())
          ->save();
        $this
          ->messenger()
          ->addStatus('Facebook authentication was successful. Access token obtained.');
      }
    }
  } catch (FacebookResponseException $e) {
    $error_msg = $this
      ->t('We received the following error while attempting to authenticate your Facebook account: @error', [
      '@error' => $e
        ->getMessage(),
    ]);
    $this
      ->messenger()
      ->addError($error_msg);
  } catch (FacebookSDKException $e) {
    $error_msg = $this
      ->t('We received the following error while attempting to authenticate your Facebook account: @error', [
      '@error' => $e
        ->getMessage(),
    ]);
    $this
      ->messenger()
      ->addError($error_msg);
  }
  return new RedirectResponse(Url::fromRoute('fb_instant_articles.api_settings_form')
    ->toString());
}