Skip to content

Instantly share code, notes, and snippets.

@PhoSor
Created May 17, 2017 13:40
Show Gist options
  • Save PhoSor/09ad2c431aa7923ee715834b68e697db to your computer and use it in GitHub Desktop.
Save PhoSor/09ad2c431aa7923ee715834b68e697db to your computer and use it in GitHub Desktop.
<?php
namespace PhoSor\Socialite;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class VkProvider extends AbstractProvider implements ProviderInterface {
protected $scopes = ['email'];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state) {
return $this->buildAuthUrlFromBase(
'https://oauth.vk.com/authorize', $state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl() {
return 'https://oauth.vk.com/access_token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token) {
$response = $this->getHttpClient()->get(
'https://api.vk.com/method/users.get?user_ids='.$token['user_id'].'&fields=uid,first_name,last_name,screen_name,photo_100,verified,city,site,has_mobile,contacts'
);
$response = json_decode($response->getBody()->getContents(), true)['response'][0];
return array_merge($response, [
'email' => array_get($token, 'email'),
]);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user) {
return (new User())->setRaw($user)->map([
'id' => $user['uid'],
'nickname' => $user['screen_name'],
'name' => $user['first_name'].' '.$user['last_name'],
'email' => array_get($user, 'email'),
'avatar' => $user['photo_100'],
]);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code) {
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
/**
* {@inheritdoc}
*/
protected function parseAccessToken($body) {
return json_decode($body, true);
}
}
<?php
namespace PhoSor\Socialite;
use Illuminate\Support\ServiceProvider;
class VkServiceProvider extends ServiceProvider
{
public function register() {}
static function boot($core)
{
$socialite = $core->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'vk',
function ($app) use ($socialite) {
$config = $app['config']['services.vk'];
return $socialite->buildProvider(VkProvider::class, $config);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment