Loading src/Controller/TwitterAuthController.php +40 −6 Changes for src/Controller/TwitterAuthController.php: 40 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\RenderContext; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\social_auth\User\UserAuthenticator; use Drupal\social_auth\SocialAuthDataHandler; Loading Loading @@ -59,6 +61,13 @@ class TwitterAuthController extends ControllerBase { */ protected $messenger; /** * The renderer service. * * @var \Drupal\Core\Render\Renderer */ protected $renderer; /** * TwitterLoginController constructor. * Loading @@ -74,25 +83,33 @@ class TwitterAuthController extends ControllerBase { * SocialAuthDataHandler object. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \Drupal\Core\Render\RendererInterface $renderer * Used to handle metadata for redirection to authentication URL. */ public function __construct(NetworkManager $network_manager, UserAuthenticator $user_authenticator, TwitterAuthManager $twitter_manager, RequestStack $request, SocialAuthDataHandler $data_handler, MessengerInterface $messenger) { MessengerInterface $messenger, RendererInterface $renderer = NULL) { $this->networkManager = $network_manager; $this->userAuthenticator = $user_authenticator; $this->twitterManager = $twitter_manager; $this->request = $request; $this->dataHandler = $data_handler; $this->messenger = $messenger; $this->renderer = $renderer; // Sets the plugin id. $this->userAuthenticator->setPluginId('social_auth_twitter'); // Sets the session keys to nullify if user could not logged in. $this->userAuthenticator->setSessionKeysToNullify(['access_token']); if (!$this->renderer) { $this->renderer = \Drupal::service('renderer'); } } /** Loading @@ -105,7 +122,8 @@ public static function create(ContainerInterface $container) { $container->get('twitter_auth.manager'), $container->get('request_stack'), $container->get('social_auth.data_handler'), $container->get('messenger') $container->get('messenger'), $container->get('renderer') ); } Loading @@ -113,6 +131,8 @@ public static function create(ContainerInterface $container) { * Redirects to Twitter for authentication. */ public function redirectToTwitter() { $context = new RenderContext(); $response = $this->renderer->executeInRenderContext($context, function () { try { /* @var \Drupal\social_auth_twitter\Plugin\Network\TwitterAuth $network_plugin */ // Creates an instance of the social_auth_twitter Network Plugin. Loading @@ -123,11 +143,14 @@ public function redirectToTwitter() { // If destination parameter is set, save it. if ($destination) { $this->userAuthenticator->setDestination($destination); $this->request->getCurrentRequest()->query->remove('destination'); } /* @var \Abraham\TwitterOAuth\TwitterOAuth $connection */ $connection = $network_plugin->getSdk(); if ($connection) { // Requests Twitter to get temporary tokens. $request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $network_plugin->getOauthCallback()]); Loading @@ -141,17 +164,28 @@ public function redirectToTwitter() { // Forces session to be saved before redirection. $this->twitterManager->save(); $response = new TrustedRedirectResponse($url); $response->send(); return new TrustedRedirectResponse($url); } else { $this->messenger->addError($this->t('Social Auth Twitter not configured properly. Contact site administrator.')); // Redirects the user to allow them to grant permissions. return $response; return $this->redirect('user.login'); } } catch (\Exception $ex) { $this->messenger->addError($this->t('You could not be authenticated, please contact the administrator.')); return $this->redirect('user.login'); } }); // Add bubbleable metadata to the response. if ($response instanceof TrustedRedirectResponse && !$context->isEmpty()) { $bubbleable_metadata = $context->pop(); $response->addCacheableDependency($bubbleable_metadata); } return $response; } /** Loading Loading
src/Controller/TwitterAuthController.php +40 −6 Changes for src/Controller/TwitterAuthController.php: 40 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\RenderContext; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Routing\TrustedRedirectResponse; use Drupal\social_auth\User\UserAuthenticator; use Drupal\social_auth\SocialAuthDataHandler; Loading Loading @@ -59,6 +61,13 @@ class TwitterAuthController extends ControllerBase { */ protected $messenger; /** * The renderer service. * * @var \Drupal\Core\Render\Renderer */ protected $renderer; /** * TwitterLoginController constructor. * Loading @@ -74,25 +83,33 @@ class TwitterAuthController extends ControllerBase { * SocialAuthDataHandler object. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \Drupal\Core\Render\RendererInterface $renderer * Used to handle metadata for redirection to authentication URL. */ public function __construct(NetworkManager $network_manager, UserAuthenticator $user_authenticator, TwitterAuthManager $twitter_manager, RequestStack $request, SocialAuthDataHandler $data_handler, MessengerInterface $messenger) { MessengerInterface $messenger, RendererInterface $renderer = NULL) { $this->networkManager = $network_manager; $this->userAuthenticator = $user_authenticator; $this->twitterManager = $twitter_manager; $this->request = $request; $this->dataHandler = $data_handler; $this->messenger = $messenger; $this->renderer = $renderer; // Sets the plugin id. $this->userAuthenticator->setPluginId('social_auth_twitter'); // Sets the session keys to nullify if user could not logged in. $this->userAuthenticator->setSessionKeysToNullify(['access_token']); if (!$this->renderer) { $this->renderer = \Drupal::service('renderer'); } } /** Loading @@ -105,7 +122,8 @@ public static function create(ContainerInterface $container) { $container->get('twitter_auth.manager'), $container->get('request_stack'), $container->get('social_auth.data_handler'), $container->get('messenger') $container->get('messenger'), $container->get('renderer') ); } Loading @@ -113,6 +131,8 @@ public static function create(ContainerInterface $container) { * Redirects to Twitter for authentication. */ public function redirectToTwitter() { $context = new RenderContext(); $response = $this->renderer->executeInRenderContext($context, function () { try { /* @var \Drupal\social_auth_twitter\Plugin\Network\TwitterAuth $network_plugin */ // Creates an instance of the social_auth_twitter Network Plugin. Loading @@ -123,11 +143,14 @@ public function redirectToTwitter() { // If destination parameter is set, save it. if ($destination) { $this->userAuthenticator->setDestination($destination); $this->request->getCurrentRequest()->query->remove('destination'); } /* @var \Abraham\TwitterOAuth\TwitterOAuth $connection */ $connection = $network_plugin->getSdk(); if ($connection) { // Requests Twitter to get temporary tokens. $request_token = $connection->oauth('oauth/request_token', ['oauth_callback' => $network_plugin->getOauthCallback()]); Loading @@ -141,17 +164,28 @@ public function redirectToTwitter() { // Forces session to be saved before redirection. $this->twitterManager->save(); $response = new TrustedRedirectResponse($url); $response->send(); return new TrustedRedirectResponse($url); } else { $this->messenger->addError($this->t('Social Auth Twitter not configured properly. Contact site administrator.')); // Redirects the user to allow them to grant permissions. return $response; return $this->redirect('user.login'); } } catch (\Exception $ex) { $this->messenger->addError($this->t('You could not be authenticated, please contact the administrator.')); return $this->redirect('user.login'); } }); // Add bubbleable metadata to the response. if ($response instanceof TrustedRedirectResponse && !$context->isEmpty()) { $bubbleable_metadata = $context->pop(); $response->addCacheableDependency($bubbleable_metadata); } return $response; } /** Loading