Laravel socialite: always grab default avatar

Viewed 1086

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?

2 Answers

Append the access_token to the query

Like so:

$social = Socialite::driver('facebook')->user();
$avatar = $social->avatar_original . "&access_token={$social->token}";

The $avatar variable will contain the correct url of the profile picture.

Related