Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Daaaaad's avatar

Socialite Facebook extra fields

Hello, I try to get extra fields from Facebook (Birthday in this case) with Socialite.

I found a method called fields() in FacebookProvider.php that should overwrite the default $fields array :

protected $fields = ['first_name', 'last_name', 'email', 'gender', 'verified'];

/**
     * Set the user fields to request from Facebook.
     *
     * @param  array  $fields
     * @return $this
     */
    public function fields(array $fields)
    {
        $this->fields = $fields;

        return $this;
    }

This method seems to be called but the user() method still gives me old data ('first_name', 'last_name', 'email', 'gender' and 'verified').

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

Any help ?

(Last version of Laravel : 5.1 and last version of Socialite : v2.0.11)

0 likes
6 replies
bobbybouwmann's avatar

You user needs to accept the permission for using the user_birthday permission. Remove the connection between your facebook account and your app in your apps view and login again, you will need to accept the permissions again, but it should now state that the birthday permission is required as well

Daaaaad's avatar

Hi bobby, thank your for your answer :D

I already give the "user_birthday" permission to the app... the problem is not there :(

I override the $fields array with my own fields ('first_name', 'last_name', 'email', 'gender', 'birthday' instead of 'first_name', 'last_name', 'email', 'gender', 'verified') using the fields() method from FacebookProvider.php just before the redirect() call :

Socialite::driver('facebook')->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->redirect();

And when comes the time to fetch user data the $fields array does not contain my own fields but the default fields...

bobbybouwmann's avatar

I get your point. I don't see how this cannot work here... the fields function is returning $this so chaining should work here. Did you try to first create the socialite provider and then redirecting?

$provider = Socialite::driver('facebook');

$provider->fields([
    'first_name', 'last_name', 'email', 'gender', 'birthday'
])->scopes([
    'email', 'user_birthday'
])->redirect();
Daaaaad's avatar

Ok, got it working :)

No, the chaining was not the problem neither... I had to specify fields also when requesting user info :

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

Here we are :

    /**
     * Obtain the user information from Facebook.
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function handleFacebookCallback()
    {
        $facebook_user = Socialite::driver('facebook')->fields([
            'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->user();

        dd($facebook_user);
}

Many thanks for your help :)

6 likes
andrei2506's avatar

Hi Daaad!

I followed this discussion and i have the same issue. I gave permission to Facebook for the app for all requested fields and scopes. But i can still not access them. Is there anything else you have changed to make it work?

1 like
azimidev's avatar

You'll need to do something like this and it perfectly works:

<?php
class FacebookLogin {
    /**
     * Since facebook will give you name, email, gender by default,
     * You'll only need to initialize Facebook scopes after getting permission
     */
    const facebookScope = [
        'user_birthday',
        'user_location',
    ];
    /**
     * Initialize Facebook fields to override
     */
    const facebookFields = [
        'name', // Default
        'email', // Default
        'gender', // Default
        'birthday', // I've given permission
        'location', // I've given permission
    ];
    public function facebookRedirect()
    {
        return Socialite::driver('facebook')->fields(self::facebookFields)->scopes(self::facebookScope)->redirect();
    }

    public function facebookCallback()
    {
        $facebook = Socialite::driver('facebook')->fields(self::facebookFields)->user();
    }
}

4 likes

Please or to participate in this conversation.