Skip to content

Instantly share code, notes, and snippets.

@kidager
Created January 21, 2019 10:57
Show Gist options
  • Save kidager/2e4b4064e15f478f43b094e7648da81c to your computer and use it in GitHub Desktop.
Save kidager/2e4b4064e15f478f43b094e7648da81c to your computer and use it in GitHub Desktop.
Workaround for Socialite ^3.0
<?php
namespace App\Managers;
use App\Providers\CustomGoogleProvider;
use Laravel\Socialite\SocialiteManager;
class CustomSocialiteManager extends SocialiteManager
{
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createGoogleDriver()
{
$config = $this->app['config']['services.google'];
return $this->buildProvider(
CustomGoogleProvider::class, $config
);
}
}
<?php
namespace App\Providers;
use App\Managers\CustomSocialiteManager;
use Illuminate\Support\ServiceProvider;
use Laravel\Socialite\Contracts\Factory;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//...
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Factory::class, function ($app) {
return new CustomSocialiteManager($app);
});
}
}
<?php
namespace App\Providers;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\GoogleProvider;
class CustomGoogleProvider extends GoogleProvider
{
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://www.googleapis.com/userinfo/v2/me?', [
'query' => [
'prettyPrint' => 'false',
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$avatarUrl = Arr::get($user, 'picture');
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => Arr::get($user, 'nickname'),
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'avatar' => $avatarUrl,
'avatar_original' => preg_replace('/\?sz=([0-9]+)/', '', $avatarUrl),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment