FacebookAuthManager.php in Social Auth Facebook 3.x
File
src/FacebookAuthManager.php
View source
<?php
namespace Drupal\social_auth_facebook;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\social_auth\AuthManager\OAuth2Manager;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Component\HttpFoundation\RequestStack;
class FacebookAuthManager extends OAuth2Manager {
protected $client;
public function __construct(ConfigFactory $configFactory, LoggerChannelFactoryInterface $logger_factory, RequestStack $request_stack) {
parent::__construct($configFactory
->get('social_auth_facebook.settings'), $logger_factory, $this->request = $request_stack
->getCurrentRequest());
}
public function authenticate() {
try {
$this
->setAccessToken($this->client
->getLongLivedAccessToken($this->client
->getAccessToken('authorization_code', [
'code' => $this->request->query
->get('code'),
])));
} catch (\Exception $e) {
$this->loggerFactory
->get('social_auth_facebook')
->error('There was an error during authentication. Exception: ' . $e
->getMessage());
}
}
public function getUserInfo() {
try {
$access_token = $this
->getAccessToken();
if (!$this->user && $access_token != NULL) {
$this->user = $this->client
->getResourceOwner($access_token);
}
else {
$this->loggerFactory
->get('social_auth_facebook')
->error('There was an error fetching the access token for user.');
}
} catch (\Exception $e) {
watchdog_exception('social_auth_facebook', $e);
}
return $this->user;
}
public function getAuthorizationUrl() {
$scopes = [
'email',
'public_profile',
];
$extra_scopes = $this
->getScopes();
if ($extra_scopes) {
$scopes = array_merge($scopes, explode(',', $extra_scopes));
}
return $this->client
->getAuthorizationUrl([
'scope' => $scopes,
]);
}
public function requestEndPoint($method, $path, $domain = NULL, array $options = []) {
if (!$domain) {
$domain = 'https://graph.facebook.com';
}
$url = $domain . '/v' . $this->settings
->get('graph_version') . $path;
$url .= '&access_token=' . $this
->getAccessToken();
try {
$request = $this->client
->getAuthenticatedRequest($method, $url, $this
->getAccessToken(), $options);
} catch (\Exception $e) {
watchdog_exception('social_auth_facebook', $e);
return NULL;
}
try {
return $this->client
->getParsedResponse($request);
} catch (IdentityProviderException $e) {
$this->loggerFactory
->get('social_auth_facebook')
->error('There was an error when requesting ' . $url . '. Exception: ' . $e
->getMessage());
}
return NULL;
}
public function getState() {
return $this->client
->getState();
}
}