Reputation: 1
I am using Hybridauth 3.9. to add "Login with Google button" on my website. After the user login is success, I am not able to fetch their firstName, lastName, etc. However I can fetch their email id only.
NOTE: My publishing status is "Testing" in Google Cloud OAuth consent screen. I hope this is not the reason.
Working code sample:
$userProfile = $adapter->getUserProfile();
echo json_encode($userProfile);
//I am only getting the email id of the user. Rest are null.
{"identifier":"*****3478","webSiteURL":null,"profileURL":null,"photoURL":"https:\/\/lh3.googleusercontent.com\/a-\/ACB-R5STKfcxZY0D0fSaRRCvuV0bYbhFLM0bjxxwAERB=s96-c","displayName":null,"description":null,"firstName":null,"lastName":null,"gender":null,"language":null,"age":null,"birthDay":null,"birthMonth":null,"birthYear":null,"email":"myemailid@gmail.com","emailVerified":"myemailid@gmail.com","phone":null,"address":null,"country":null,"region":null,"city":null,"zip":null,"data":[]}
Minimal reproducible example
require 'path/to/vendor/autoload.php';
use Hybridauth\Hybridauth;
$config = [
'callback' => 'https://path/to/hybridauth/examples/example_07/callback.php',
'providers' => [
'Google' => [
'enabled' => true,
'keys' => [
'id' => '...',
'secret' => '...',
],
'scope' => 'email',
],
],
];
$hybridauth = new Hybridauth($config);
$adapters = $hybridauth->getConnectedAdapters();
<?php foreach ($hybridauth->getProviders() as $name) : ?>
<?php if (!isset($adapters[$name])) : ?>
<li>
<a href="#" onclick="javascript:auth_popup('<?php print $name ?>');">
Sign in with <?php print $name ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
<?php if ($adapters) : ?>
<h1>You are logged in:</h1>
<ul>
<?php foreach ($adapters as $name => $adapter) : ?>
echo $adapter->getUserProfile()->firstName;// getting null here
echo $adapter->getUserProfile()->email;// email is echoing fine
<?php endforeach; ?>
</ul>
<?php endif; ?>
<script>
function auth_popup(provider) {
var authWindow = window.open('https://www.*****.com/social_login_callback?provider=' + provider, 'authWindow', 'width=600,height=400,scrollbars=yes');
window.closeAuthWindow = function () {
authWindow.close();
}
return false;
}
</script>
callback.php
<?php
require 'path/to/vendor/autoload.php';
use Hybridauth\Exception\Exception;
use Hybridauth\Hybridauth;
use Hybridauth\HttpClient;
use Hybridauth\Storage\Session;
$config = [
'callback' => 'https://path/to/hybridauth/examples/example_07/callback.php',
'providers' => [
'Google' => [
'enabled' => true,
'keys' => [
'id' => '...',
'secret' => '...',
],
'scope' => 'email',
],
];
try {
$hybridauth = new Hybridauth($config);
$storage = new Session();
$error = false;
//
// Event 1: User clicked SIGN-IN link
//
if (isset($_GET['provider'])) {
// Validate provider exists in the $config
if (in_array($_GET['provider'], $hybridauth->getProviders())) {
// Store the provider for the callback event
$storage->set('provider', $_GET['provider']);
} else {
$error = $_GET['provider'];
}
}
//
// Event 2: Provider returns via CALLBACK
//
if ($provider = $storage->get('provider')) {
$hybridauth->authenticate($provider);
$storage->set('provider', null);
// Retrieve the provider record
$adapter = $hybridauth->getAdapter($provider);
$userProfile = $adapter->getUserProfile();
$accessToken = $adapter->getAccessToken();
// add your custom AUTH functions (if any) here
// ...
$data = [
'token' => $accessToken,
'identifier' => $userProfile->identifier,
'email' => $userProfile->email,
'first_name' => $userProfile->firstName,
'last_name' => $userProfile->lastName,
'photoURL' => strtok($userProfile->photoURL, '?'),
];
// ...
// Close pop-up window
echo "
<script>
if (window.opener.closeAuthWindow) {
window.opener.closeAuthWindow();
}
</script>";
}
} catch (Exception $e) {
error_log($e->getMessage());
echo $e->getMessage();
}
Please suggest. Thanks.
Upvotes: 0
Views: 159