Skip to content

Instantly share code, notes, and snippets.

@orobogenius
Created April 9, 2019 23:12
Show Gist options
  • Save orobogenius/8d3256cfb5b4f9b2870dcaf3e79c3fc6 to your computer and use it in GitHub Desktop.
Save orobogenius/8d3256cfb5b4f9b2870dcaf3e79c3fc6 to your computer and use it in GitHub Desktop.
<?php
namespace App\OAuth;
use App\Repositories\UserRepository;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Passport\Bridge\User as UserEntity;
use League\OAuth2\Server\Exception\OAuthServerException;
class SocialUserProvider implements SocialUserProviderInterface
{
/**
* The user repository instance.
*
* @var UserRepository
*/
protected $userRepository;
/**
* Create a social user provider instance.
*
* @param UserRepository $userRepository
*/
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* {@inheritdoc}
*/
public function getUserEntityByAccessToken($provider, $accessToken)
{
$user = $this->getUserFromSocialProvider($provider, $accessToken);
if (! $user) {
return;
}
return new UserEntity($user->getAuthIdentifier());
}
/**
* Get the user from the specified provider using the given access token.
*
* @param string $provider
* @param string $accessToken
*
* @throws \League\OAuth2\Server\Exception\OAuthServerException
*
* @return \App\User
*/
public function getUserFromSocialProvider($provider, $accessToken)
{
try {
$user = Socialite::driver($provider)->userFromToken($accessToken);
} catch (\Exception $ex) {
throw new OAuthServerException(
'Authentication error, invalid access token',
$errorCode = 400,
'invalid_request'
);
}
return $this->userRepository->findOrCreateSocialUser(
array_merge($user->getRaw(), ['provider' => $provider])
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment