Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Socialite first and last name

I'm adding social authentication to an application using Laravel's Socialite. I can retrieve the full name but not the first and last names separately. After the callback happens and Socialite is handling it, the user is retrieved successfully. If I am to dump the user I get back from $user = this->social->driver('facebook')->user(); I get the following:

object(Laravel\Socialite\Two\User)#459 (8) {
     ["token" ]=> string(209) "{token}"
     ["id"] => string(17) "{socialID}"
     ["nickname"] => NULL
     ["name"] => string(14) "{Full Name}"
     ["email"] => string(19) "{Email address}"
     ["avatar"] => string(69) "https://graph.facebook.com/v2.4/{socialID}/picture?type=normal"
     ["user"] => array(6) {
        ["first_name"] => string(6) "{First name}"
        ["last_name"] => string(7) "{Last mame}"
        ["email"] => string(19) "{Email address}"
        ["gender"] => string(4) "male"
        ["verified"] => bool(true)
        ["id"] => string(17) "{socialID}"
    }
    ["avatar_original"] => string(68) "https://graph.facebook.com/v2.4/{socialID}/picture?width=1920"

}

I can obtain the full name or email via $user->name or $user->email however, I can not get the separate first and last names. I have tried $user->first_name as well as trying to dump the $user->user array but all I see is undefined property errors.

I do not want to do something weird like extract it from the full name when the separate first and last name are clearly there as it can get ugly when middle names are present.

I have Googled my way around and weirdly, nobody came across this issue. Am I missing something from the docs? Any suggestions on how to retrieve the first and last name from the user array are greatly appreciated.

like image 851
junkystu Avatar asked Sep 04 '15 22:09

junkystu


People also ask

What is Socialite laravel 8?

Laravel Socialite provides an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, LinkedIn, GitHub, GitLab and Bitbucket. It handles almost all of the boilerplate social authentication code you are dreading writing.

Does Google use laravel?

Introduction. In addition to typical, form based authentication, Laravel also provides a simple, convenient way to authenticate with OAuth providers using Laravel Socialite. Socialite currently supports authentication via Facebook, Twitter, LinkedIn, Google, GitHub, GitLab, and Bitbucket.


2 Answers

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']

Other stuff you can ask for:

https://developers.facebook.com/docs/graph-api/reference/user/

for google plus:

$user->firstname = $user->user['name']['givenName'];
$user->lastname = $user->user['name']['familyName'];
like image 138
Richard Torcato Avatar answered Sep 27 '22 17:09

Richard Torcato


According to the dump of the $user var you should be able to access the name values by doing:

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

like image 43
omma2289 Avatar answered Sep 27 '22 17:09

omma2289