Search code examples
facebooklaravel-6laravel-socialite

Laravel socialite: always grab default avatar


I'm allowing users to register using Scialite (Still working locally on Laravel homestead with openSSL enabled). it works fine with FB except the avatar it always get the default image. I'm testing with the same FB account that created the app (FB app is in development mode)

enter image description here

Code

public function handleProviderCallback($provider)
{
    $userSocial =   Socialite::driver($provider)->user();
    $users       =   User::where(['email' => $userSocial->getEmail()])->first();
    if($users){
        Auth::login($users);
        return redirect('/');
    }else{
        $avatar_file_name = 'test';
        if(!empty($userSocial->getAvatar()) && $userSocial->getAvatar()!='' && $userSocial->getAvatar() != null)
        {
            $fileContents = file_get_contents($userSocial->getAvatar());
            File::put(public_path() . '/uploads/' . $userSocial->getId() . ".jpg", $fileContents);
            $avatar_file_name =  $userSocial->getId() . ".jpg";
        }

        $user = User::create([
            'name'          => $userSocial->getName(),
            'email'         => $userSocial->getEmail(),
            'avatar'         => $avatar_file_name,
            'provider_id'   => $userSocial->getId(),
            'provider'      => $provider,
        ]);
        Auth::login($user, true);
        return redirect()->route('home');
    }
}

When I dd($userSocial->getAvatar()) and copy the URL it opens the default avatar (the above image) why is that? Is there anything I have to do the the FB App or login code to get the real avatar of the user?


Solution

  • I've encountered this issue now. Socialite avatar retrieving was working fine around one month ago. But it is broken when I check it few days ago. So current solution is just appended access_token to retrieve image something like below.

    https://graph.facebook.com/v3.3/[facebook_user_id]/picture?type=normal&access_token=[fbtoken]

    please let me know if you find more easy solution.

    result is:

    enter image description here