Laravel Socialite first and last name

phplaravelnamelastfirstsocialiteio

Last Update : 2023-09-22 UTC 10:49:24 AM

Answers of > Laravel Socialite first and last name

You can also use offsetGet() method which is w3coded php first defined in AbstractUser class of socialite w3coded php first package, which returns $this->user[$offset]; so w3coded php first using it you can get first name and last name, w3coded php first ,I'm adding social authentication to an w3coded php first application using Laravel's Socialite. I can w3coded php first retrieve the full name but not the first and w3coded php first last names separately. After the callback w3coded php first happens and Socialite is handling it, the user w3coded php first is retrieved successfully. If I am to dump the w3coded php first user I get back from $user = w3coded php first this->social->driver('facebook')->user(); I get w3coded php first the following:, w3coded php first w3coded php first Since different providers w3coded php first provide the first and last name values in w3coded php first different ways, isn't there a consisten way of w3coded php first getting the first and last name as separated w3coded php first values with Socialite? Something like w3coded php first $user->getFirstname and $user->getLastname. w3coded php first – Camilo w3coded php first Mar 3 '16 at 20:21 w3coded php first , w3coded php first What kind of substance that existed in w3coded php first the middles ages could, when ignited, w3coded php first potentially destroy everything within several w3coded php first miles?

I've found that sometimes the user object won't contain the first and last names unless you specify you need those fields.

//get the driver and set desired fields
$driver = Socialite::driver('facebook')
                ->fields([
                    'name', 
                    'first_name', 
                    'last_name', 
                    'email', 
                    'gender', 
                    'verified'
                ]);
// retrieve the user
$user = $driver->user();

then you can get the first name and last name like this

$user->user['first_name'] and $user->user['last_name']

for google plus:

$user->firstname = $user->user['name']['givenName'];
$user->lastname = $user->user['name']['familyName'];

For GOOGLE & FACEBOOK

$providerUser = Socialite::driver($provider)->user(); // $provider can be facebook, google etc.

switch($provider){
   case 'facebook':
      $first_name = $providerUser->offsetGet('first_name');
      $last_name = $providerUser->offsetGet('last_name');
      break;

   case 'google':
      $first_name = $providerUser->offsetGet('given_name');
      $last_name = $providerUser->offsetGet('family_name');
      break;

 // You can also add more provider option e.g. linkedin, twitter etc.

   default:
      $first_name = $providerUser->getName();
      $last_name = $providerUser->getName();
}

$user = User::create([
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $providerUser->getEmail(),
                'image' => $providerUser->getAvatar(),
            ]);

In linkedin you can get first and last name from provider like this.

$linkedinUser = $this->socialite->driver('linkedin')->user());

$attributes = [
        'first_name' => $linkedinUser->user['firstName'],
        'last_name' => $linkedinUser->user['lastName'],
        'email' => $linkedinUser->email,
        'avatar' => $linkedinUser->avatar,
        'linkedin_id' => $linkedinUser->id
    ];

What i did, was to create a method (getFirstLastNames()) that will get that "name" value and break it into first_name and last_name by exploding them when a space or multiple spaces is detected. Then i use them to populate my users table:

protected function getFirstLastNames($fullName)
{
    $parts = array_values(array_filter(explode(" ", $fullName)));

    $size = count($parts);

    if(empty($parts)){
        $result['first_name']   = NULL;
        $result['last_name']    = NULL;
    }

    if(!empty($parts) && $size == 1){
        $result['first_name']   = $parts[0];
        $result['last_name']    = NULL;
    }

    if(!empty($parts) && $size >= 2){
        $result['first_name']   = $parts[0];
        $result['last_name']    = $parts[1];
    }

    return $result;
}

The $fullName variable is:

Socialite::driver($provider)->getName();

Now that you have the data in the array you can use them while creating the user:

$userFirstLastName = $this->getFirstLastNames(Socialite::driver($provider)->getName());

$user = User::create([
            'email'         => Socialite::driver($provider)->getName()->getEmail(),
            'first_name'    => $userFirstLastName['first_name'],
            'last_name'     => $userFirstLastName['last_name'],
        ]);

And you could simply do:

$NameArray = explode(' ',$user->getName());
$First_name = $NameArray[0];
$Last_name = $NameArray[1];

Current topics : Laravel Socialite first and last name

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:44:11 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:44:05 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:43:45 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:43:35 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:43:30 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:43:16 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:43:04 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:42:57 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:42:42 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:42:24 PM

Top
© 2023 W3CODED - All Rights Reserved.