Drupal Google Login get user Gender, Phone Number etc

38 views Asked by At

I want to get user Phone number & Gender when the user logged in using Google SSO in Drupal 9. I have created a event subscriber to get the user name to set it as first & last name in drupal user fields.

public function onUserCreated(UserEvent $event) {
    
    $this->dataHandler->setSessionPrefix($event->getPluginId());

    $client = $this->networkManager->createInstance($event->getPluginId())->getSdk();

    $this->providerAuth->setClient($client)->setAccessToken($this->dataHandler->get('access_token'));
    $this->providerAuth->addScope('profile');
    $userInfo = $this->providerAuth->getUserInfo();
    $userFullName = $userInfo->getName();
    $userFirstName = trim(strtok($userFullName, ' '));
    $userlastname = trim(strstr($userFullName, ' '));
    $user = $event->getUser();
    $user->set('field_first_name', $userFirstName);
    $user->set('field_last_name', $userlastname);
    $user->set('field_user_full_name', $userFullName);
    $user->save();
  }

For debug purpose when I checked the output of $userInfo, I get an array like this:

Drupal\social_auth\User\SocialAuthUser Object
(
    [firstName:protected] => 
    [lastName:protected] => 
    [name:protected] => John Doe
    [email:protected] => [email protected]
    [providerUserID:protected] => 106216096099039026764
    [token:protected] => ya29.a0AfB_byA0lYD30jeBstnhcgo8Lq4XRhQhkf7TztPYuRFExGI8OL1CHlsOyPIdIUGzCPiy7hoYPyF5sgosjlxyPm_S2Jtsd_xBipQGH3zm4tLZM5P2XxLe0YxoqH-Xc6MljEX5K0zsBWC9y_y2TBL90p-lRRQqwjdG7iAaCgYKAW4SAQ8SFQHGX2MiUJKqXslD8_L6-GpLptlgNA0170
    [pictureUrl:protected] => https://lh3.googleusercontent.com/a/ACg8ocJx-6PLojggHSp4CHRW4ED5d6GdJptP5JrWrCg9A=s96-c
    [picture:protected] => 
    [additionalData:protected] => 
    [customData:protected] => Array
        (
        )

)

As you can see, the additionalData & customData are empty. How will get user additional data like Phone, Gender, DOB etc?

0

There are 0 answers