Reputation: 75
I just made an auth controller with Laravel Socialite. First time I just retrieve the name and email data, and then I try to to retrieve the gender but it comes with an error that says
Undefined property: Laravel\Socialite\Two\User::$gender
Someone please help me to solve my problem, oh yeah my web still in development mode but I think it's not a problem because in facebook developer documentation said I still have access to basic information like first name, last name, profile picture, gender and age range.
Controller Source Code:
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
try {
$user = Socialite::driver('facebook')->user();
} catch (Exception $e) {
return Redirect::to('login/facebook');
}
$authUser = $this->findOrCreateUser($user);
Auth::login($authUser, true);
return Redirect::to('home');
}
private function findOrCreateUser($fbUser)
{
if ($authUser = User::where('email', $fbUser->email)->first()) {
return $authUser;
}
$role = "member";
return User::create([
'firstname' => $fbUser->name,
'email' => $fbUser->email,
'gender' => $fbUser->gender,
'role' => $role
]);
}
Here I provide what i've got if i use dd($user);
method :
User {# ▼
+token: "tokken value"
+id: "id value"
+nickname: null
+name: "name value"
+email: "email value"
+avatar: "avatar value"
+"user": array:value [▶]
+"avatar_original": "avatar_original value"
}
Upvotes: 3
Views: 1210
Reputation: 848
'gender' => $fbUser->user['gender'],
Because gender comes in user[] array... so you access to it that way.
But remember you'll need to get the Facebook permission for your app to retrieve this value.
Upvotes: 0