You are here

public function BynderOAuthLogin::oAuth in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/Controller/BynderOAuthLogin.php \Drupal\bynder\Controller\BynderOAuthLogin::oAuth()
  2. 8.2 src/Controller/BynderOAuthLogin.php \Drupal\bynder\Controller\BynderOAuthLogin::oAuth()
  3. 4.0.x src/Controller/BynderOAuthLogin.php \Drupal\bynder\Controller\BynderOAuthLogin::oAuth()

The oAuth login controller.

File

src/Controller/BynderOAuthLogin.php, line 64

Class

BynderOAuthLogin
Controller for oAuth login.

Namespace

Drupal\bynder\Controller

Code

public function oAuth(Request $request) {
  try {
    if ($request->query
      ->get('oauth_token')) {
      $this->bynder
        ->finishOAuthTokenRetrieval($request);
      return [
        '#markup' => '<script>window.close()</script>',
        '#allowed_tags' => [
          'script',
        ],
      ];
    }
    else {
      $url = $this->bynder
        ->initiateOAuthTokenRetrieval();
      $response = new TrustedRedirectResponse($url
        ->toString(), SymfonyResponse::HTTP_SEE_OTHER);
      $response
        ->setMaxAge(-1);
      return $response;
    }
  } catch (GuzzleException $e) {
    (new UnableToConnectException())
      ->displayMessage();
    $this->logger
      ->get('bynder')
      ->error('Bynder OAuth login failed: @message', [
      '@message' => $e
        ->getMessage(),
    ]);
    return [];
  }
}