2

I just started using laravel, I am using laravel socialite.

I am able to get username and other data by using following code. But how do I get other data like birthday, gender, religion etc that is not in $user variable ??

my code

$user = Socialite::with('facebook')->user();

return "<h2> User Name ".$user->getName()."</h2><h2> ID :".$user->getId()."</h2><h2>Full Object: ".var_dump($user)"About".$user->getAbout();

my output:

object(Laravel\Socialite\Two\User)#170 (10) { ["token"]=> string(162) "EAADg3CcIylABAESNrt1ZBsHiLzlR7uu4e4SCjNsU8jJK6zAurEZBGW7BexQiMjiRkrIw57LYPuNrFZBiaW8MIWAh0oIOIIyFF916PH8SDCtU68r83OYBsXbJzS5x4AzIQlpxX8W7V6Mtg446CqwocGXZCHXRFv8ZD" ["refreshToken"]=> NULL ["expiresIn"]=> string(7) "5170770" ["id"]=> string(15) "624135571074425" ["nickname"]=> NULL ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "[email protected]" ["avatar"]=> string(67) "https://graph.facebook.com/v2.6/624135571074425/picture?type=normal" ["user"]=> array(5) { ["name"]=> string(21) "Muhammad Abdullah Ali" ["email"]=> string(27) "[email protected]" ["gender"]=> string(4) "male" ["verified"]=> bool(true) ["id"]=> string(15) "624135571074425" } ["avatar_original"]=> string(66) "https://graph.facebook.com/v2.6/624135571074425/picture?width=1920" }

User Name Muhammad Abdullah Ali

ID :624135571074425

2 Answers 2

11

In socialite redirect to facebook method :

    public function redirectToFacebook()
    {
        return Socialite::driver('facebook')->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->redirect();
    }

To get the birth date.

Then to use it in your own models :

    public function handleFacebookCallback()
    {
        $facebook_user = Socialite::driver('facebook')->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->user();
    }

Note: it will ask for the user to accept sharing their birth date when they connect to Facebook though.

0
0

It worked for me too. Please note its issue at socialite2.0 for laravel 5.2 Just download and update the code. https://github.com/laravel/socialite/blob/2.0/src/Two/FacebookProvider.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.