FacebookAuthController.php in Social Auth Facebook 8
File
src/Controller/FacebookAuthController.php
View source
<?php
namespace Drupal\social_auth_facebook\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\social_api\Plugin\NetworkManager;
use Drupal\social_auth\SocialAuthUserManager;
use Drupal\social_auth_facebook\FacebookAuthManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\social_auth_facebook\FacebookAuthPersistentDataHandler;
use Symfony\Component\HttpFoundation\RequestStack;
class FacebookAuthController extends ControllerBase {
private $networkManager;
private $userManager;
private $facebookManager;
private $request;
private $persistentDataHandler;
public function __construct(NetworkManager $network_manager, SocialAuthUserManager $user_manager, FacebookAuthManager $facebook_manager, RequestStack $request, FacebookAuthPersistentDataHandler $persistent_data_handler) {
$this->networkManager = $network_manager;
$this->userManager = $user_manager;
$this->facebookManager = $facebook_manager;
$this->request = $request;
$this->persistentDataHandler = $persistent_data_handler;
$this->userManager
->setPluginId('social_auth_facebook');
$this->userManager
->setSessionKeysToNullify([
$this->persistentDataHandler
->getSessionPrefix() . 'access_token',
]);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.network.manager'), $container
->get('social_auth.user_manager'), $container
->get('social_auth_facebook.manager'), $container
->get('request_stack'), $container
->get('social_auth_facebook.persistent_data_handler'));
}
public function redirectToFb() {
$facebook = $this->networkManager
->createInstance('social_auth_facebook')
->getSdk();
if (!$facebook) {
drupal_set_message($this
->t('Social Auth Facebook not configured properly. Contact site administrator.'), 'error');
return $this
->redirect('user.login');
}
$this->facebookManager
->setClient($facebook);
$fb_login_url = $this->facebookManager
->getFbLoginUrl();
if ($this->persistentDataHandler
->get('reprompt')) {
$fb_login_url = $this->facebookManager
->getFbReRequestUrl();
}
return new TrustedRedirectResponse($fb_login_url);
}
public function returnFromFb() {
$error = $this->request
->getCurrentRequest()
->get('error');
if ($error == 'access_denied') {
drupal_set_message($this
->t('You could not be authenticated.'), 'error');
return $this
->redirect('user.login');
}
$facebook = $this->networkManager
->createInstance('social_auth_facebook')
->getSdk();
if (!$facebook) {
drupal_set_message($this
->t('Social Auth Facebook not configured properly. Contact site administrator.'), 'error');
return $this
->redirect('user.login');
}
$this->facebookManager
->setClient($facebook)
->authenticate();
if (!$this->facebookManager
->checkPermission('email')) {
drupal_set_message($this
->t('Facebook login failed. This site requires permission to get your email address from Facebook. Please try again.'), 'error');
$this->persistentDataHandler
->set('reprompt', TRUE);
return $this
->redirect('user.login');
}
if (!($fb_profile = $this->facebookManager
->getUserInfo())) {
drupal_set_message($this
->t('Facebook login failed, could not load Facebook profile. Contact site administrator.'), 'error');
return $this
->redirect('user.login');
}
if (!($email = $this->facebookManager
->getEmail($fb_profile))) {
drupal_set_message($this
->t('Facebook login failed. This site requires permission to get your email address.'), 'error');
return $this
->redirect('user.login');
}
$this->persistentDataHandler
->set('access_token', $this->facebookManager
->getAccessToken());
return $this->userManager
->authenticateUser($email, $fb_profile
->getField('name'), $fb_profile
->getField('id'), $this->facebookManager
->getFbProfilePicUrl());
}
}