Created
January 21, 2019 10:57
-
-
Save kidager/2e4b4064e15f478f43b094e7648da81c to your computer and use it in GitHub Desktop.
Workaround for Socialite ^3.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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