Search code examples
phplaravellaravel-5laravel-socialite

How to override native socialite provider?


As I am interested in getting the gender info form social network when user register, I've noticed that gender mapping is missing in the native Laravel socialite provider, for ex:

GoogleProvider.php

protected function mapUserToObject(array $user)
    {
        return (new User)->setRaw($user)->map([
            'id' => $user['id'], 'nickname' => array_get($user, 'nickname'), 'name' => $user['displayName'],
            'email' => $user['emails'][0]['value'], 'avatar' => array_get($user, 'image')['url'],
        ]);
    }

So, I can't just edit the above method and map the gender field, because GoogleProvider.php is located in the vendor folder.

The question is, how would I override mapUserToObject in different social providers afforded by Laravel?

Or, what is the recommended approach in such case?


Solution

  • Providing Google passes a gender field you should be able to access it like...

    $user = Socialite::driver('google')->user();
    echo $user['gender'];