Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FacebookProvider for Socialite October 2020
<?php
/*
Author: Gkiokan Sali
Date: 2020-10-23
Comment: This is a working updated Facebook Provider for the Laraval Package Socialite.
It inherits the new changes on the User Image Endpoint and returns the new full User Image path
according to the new specs of fb with an access_token.
https://developers.facebook.com/docs/facebook-login/access-tokens#clienttokens
https://developers.facebook.com/docs/graph-api/reference/user/picture/
*/
namespace Gkiokan\Auth\Http\Controllers\Socialite;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
// use Gkiokan\Auth\Models\User;
use Laravel\Socialite\Two\FacebookProvider as Provider;
use Laravel\Socialite\Two\User;
use Http;
use Arr;
class FacebookProvider extends Provider
{
protected $version = 'v8.0';
protected $fields = ['name', 'email', 'gender', 'verified', 'link', 'picture'];
protected function mapUserToObject(array $user)
{
// latest orginal
// $avatarUrl = $this->graphUrl.'/'.$this->version.'/'.$user['id'].'/picture';
$avatarUrl = $this->getFacebookAvatar($user);
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => null,
'name' => $user['name'] ?? null,
'email' => $user['email'] ?? null,
'avatar' => $avatarUrl, // .'?type=normal&access_token=' . $access_token,
'avatar_original' => $avatarUrl, // .'?width=1920&accss_token=' . $access_token,
'profileUrl' => $user['link'] ?? null,
]);
}
public function getAccessToken(){
$res = Http::get("https://graph.facebook.com/oauth/access_token", [
"client_id" => config('services.facebook.client_id'),
"client_secret" => config('services.facebook.client_secret'),
"grant_type" => "client_credentials",
]);
$r = $res->json();
return Arr::get($r, 'access_token', false);
}
public function getFacebookAvatar(array $user){
$access_token = $this->getAccessToken();
$path = $this->graphUrl.'/'.$this->version.'/'.$user['id'].'/picture?type=large&redirect=false&access_token=' . $access_token;
$res = Http::get($path);
return Arr::get($res->json(), 'data.url', false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment