Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Created May 27, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cannonb4ll/bdcaa86612b6a83313a9a087fdc37d25 to your computer and use it in GitHub Desktop.
Save Cannonb4ll/bdcaa86612b6a83313a9a087fdc37d25 to your computer and use it in GitHub Desktop.
// Inside appserviceprovider:
private function bootPloiSocialite()
{
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'ploi',
function ($app) use ($socialite) {
$config = $app['config']['services.ploi'];
return $socialite->buildProvider(PloiProvider::class, $config);
}
);
}
<?php
namespace App\SocialProviders;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class PloiProvider extends AbstractProvider implements ProviderInterface
{
protected $baseUrl = 'http://b9bee745.ngrok.io';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->baseUrl . '/oauth/token';
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_add(
parent::getTokenFields($code), 'grant_type', 'authorization_code'
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->baseUrl . '/api/user', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* Format the given scopes.
*
* @param array $scopes
* @param string $scopeSeparator
*
* @return string
*/
protected function formatScopes(array $scopes, $scopeSeparator)
{
return implode($scopeSeparator, $scopes);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$user = array_get($user, 'data');
return (new User)->setRaw($user)->map([
'id' => $user['email'],
'name' => $user['name'],
'email' => $user['email'],
'avatar' => $user['avatar'],
'country' => $user['country'],
'timezone' => $user['timezone'],
'plan_expires_at' => $user['plan_expires_at']
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment