Skip to content

Commit

Permalink
PHP 8 support (fix #65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesedmonston committed Aug 14, 2021
1 parent 6907e6e commit 71bc253
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
}
],
"require": {
"abraham/twitteroauth": "^1.0",
"abraham/twitteroauth": "^3.1",
"craftcms/cms": "^3.5.0",
"facebook/graph-sdk": "^5.7",
"google/apiclient": "^2.8",
"lcobucci/jwt": "^3.4"
"google/apiclient": "^2.10",
"lcobucci/jwt": "^4.1",
"league/oauth2-facebook": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 18 additions & 14 deletions src/services/FacebookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use craft\events\RegisterGqlQueriesEvent;
use craft\services\Gql;
use craft\services\UserGroups;
use Facebook\Facebook;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\Type;
use jamesedmonston\graphqlauthentication\gql\Auth;
use jamesedmonston\graphqlauthentication\GraphqlAuthentication;
use League\OAuth2\Client\Provider\Facebook;
use yii\base\Event;

class FacebookService extends Component
Expand Down Expand Up @@ -59,14 +59,15 @@ public function registerGqlQueries(RegisterGqlQueriesEvent $event)
$settings = GraphqlAuthentication::$settings;

$client = new Facebook([
'app_id' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId),
'app_secret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret),
'clientId' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId),
'clientSecret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret),
'redirectUri' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl),
'graphApiVersion' => 'v2.10',
]);

$url = $client->getRedirectLoginHelper()->getLoginUrl(
GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl),
['email']
);
$url = $client->getAuthorizationUrl([
'scope' => ['email'],
]);

return $url;
},
Expand Down Expand Up @@ -170,19 +171,22 @@ protected function _getUserFromToken(string $code): array
$errorService = GraphqlAuthentication::$errorService;

$client = new Facebook([
'app_id' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId),
'app_secret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret),
'clientId' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId),
'clientSecret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret),
'redirectUri' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl),
'graphApiVersion' => 'v2.10',
]);

$redirectUrl = GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl);
$accessToken = $client->getOAuth2Client()->getAccessTokenFromCode($code, $redirectUrl);
$accessToken = $client->getAccessToken('authorization_code', [
'code' => $code,
]);

if (!$accessToken) {
$errorService->throw($settings->invalidOauthToken, 'INVALID');
}

$user = $client->get('/me?fields=id,name,email', $accessToken->getValue())->getGraphUser();
$email = $user['email'];
$user = $client->getResourceOwner($accessToken);
$email = $user->getEmail();

if (!$email || !isset($email)) {
$errorService->throw($settings->emailNotInScope, 'INVALID');
Expand All @@ -196,7 +200,7 @@ protected function _getUserFromToken(string $code): array
);
}

$name = explode(' ', $user['name'] ?? '', 1);
$name = explode(' ', $user->getName() ?? '', 1);
$firstName = $name[0] ?? '';
$lastName = $name[1] ?? '';

Expand Down

0 comments on commit 71bc253

Please sign in to comment.