Skip to content

Commit

Permalink
Merge pull request #27 from somarkn99/main
Browse files Browse the repository at this point in the history
Fix Response by @somar Kesen
  • Loading branch information
somarkn99 committed Feb 13, 2024
2 parents d150346 + db033ae commit 4ed2fb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Platforms/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public function mapUserDataByScopes($scopes, $userData)
// Maps API response fields to more friendly names.
$fieldMappings = [
'sub' => 'id',
'email' => 'emailAddress',
'email' => 'email',
'given_name' => 'firstName',
'family_name' => 'lastName',
'picture' => 'profilePicture'
'picture' => 'picture'
];

// Determines which fields to include in the mapped data based on requested scopes.
Expand Down
30 changes: 17 additions & 13 deletions src/Platforms/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class LinkedIn extends AbstractPlatform
{
// Define the scopes that will be used to request user information. These should align with LinkedIn API permissions.
protected $scopes = ['openid', 'profile', 'email'];
protected $scopes = ['openid', 'name', 'email', 'picture'];

// Base URL for LinkedIn's API to fetch user information.
protected $baseUrl = 'https://api.linkedin.com/v2/userinfo';
Expand All @@ -34,15 +34,9 @@ class LinkedIn extends AbstractPlatform
public function getUserInfo()
{
try {
// Log the base URL for debugging purposes.
Log::debug($this->baseUrl);

// Send a GET request to LinkedIn API and store the response.
$response = $this->sendRequest($this->baseUrl, 'GET');

// Log the API response for debugging.
Log::debug(json_encode($response));

// Check if the expected user identifier 'sub' is present in the response.
if (isset($response['sub'])) {
// Map the raw API response data to a structured format based on the specified scopes.
Expand Down Expand Up @@ -73,20 +67,30 @@ public function mapUserDataByScopes($scopes, $userData)
foreach ($scopes as $scope) {
switch ($scope) {
case 'openid':
$requiredFields = array_merge($requiredFields, ['id']);
// Directly add 'sub' to 'requiredFields' to ensure it's included in the response.
$requiredFields['sub'] = 'id'; // Map 'sub' as 'id' in the mapped data
break;
case 'email':
$requiredFields = array_merge($requiredFields, ['id', 'emailAddress']);
$requiredFields['email'] = 'email';
break;
case 'profile':
$requiredFields = array_merge($requiredFields, ['id', 'firstName', 'lastName', 'profilePicture']);
case 'name':
$requiredFields['name'] = 'name';
break;
case 'picture':
$requiredFields['picture'] = 'picture';
break;
}
}

// Map each required field from the raw API response to the structured mapped data array.
foreach ($requiredFields as $field) {
$mappedData[$field] = $userData[$field] ?? null;
foreach ($requiredFields as $apiField => $mappedField) {
if ($apiField === 'sub') {
// If the field is 'sub', map it to 'id' in the final response
$mappedData['id'] = $userData[$apiField] ?? null;
} else {
// For all other fields, map them directly
$mappedData[$mappedField] = $userData[$apiField] ?? null;
}
}

return $mappedData;
Expand Down

0 comments on commit 4ed2fb8

Please sign in to comment.