coderello/laravel-passport-social-grant

Social Grant for Laravel Passport
711,072 179
Install
composer require coderello/laravel-passport-social-grant
Latest Version:4.0.0
PHP:^8.2
License:MIT
Last Updated:Jul 13, 2025
Links: GitHub  ·  Packagist
Maintainer: coderello

Laravel Passport Social Grant

Packagist GitHub tag License Downloads tests

This package adds a social grant for your OAuth2 server. It can be useful if have an API and want to provide the ability for your users to login/register through social networks.

As a result you will be able to exchange access_token, issued by the OAuth2 server of any social provider, to access_token and refresh_token issued by your own OAuth2 server. You will receive this access_token and return the user instance that corresponds to it on your own.

Installation

You can install this package via composer using this command:

composer require coderello/laravel-passport-social-grant

The package will automatically register itself.

Configuring

As the first step, you need to implement SocialUserResolverInterface:

Here is an example using Socialite -

<?php

namespace App\Resolvers;

use Coderello\SocialGrant\Resolvers\SocialUserResolverInterface;
use Illuminate\Contracts\Auth\Authenticatable;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as ProviderUser;
use League\OAuth2\Server\Entities\ClientEntityInterface;

class SocialUserResolver implements SocialUserResolverInterface
{
    /**
     * Resolve user by provider credentials.
     */
    public function resolveUserByProviderCredentials(string $provider, string $accessToken, ClientEntityInterface $client): ?Authenticatable
    {
        // Return the user that corresponds to provided credentials.
        // If the credentials are invalid, then return NULL.
        // You can use $client to allow different providers per client, e.g. $client->getIdentifier()
         $providerUser = Socialite::driver($provider)->userFromToken($accessToken);
         
         return $this->findOrCreateUser($provider, $providerUser);
    }
    
    protected function findOrCreateUser(string $provider, ProviderUser $providerUser): ?Authenticatable
    {
        // todo your logic here      
        // $email = $providerUser->getEmail();
    }
}

The next step is to bind SocialUserResolverInterface to your implementation.

You can do it by adding the appropriate key-value pair to $bindings property in AppServiceProvider:

<?php

namespace App\Providers;

use App\Resolvers\SocialUserResolver;
use Coderello\SocialGrant\Resolvers\SocialUserResolverInterface;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * All the container bindings that should be registered.
     */
    public $bindings = [
        SocialUserResolverInterface::class => SocialUserResolver::class,
    ];
}

Finally, allow the social grant on every client that should use it. Passport v13 rejects any grant that is not listed in the client's grant_types column, and responds with an unauthorized_client error.

use Laravel\Passport\Client;

$client = Client::findOrFail($clientId);
$client->grant_types = array_unique([...$client->grant_types, 'social']);
$client->save();

[!WARNING] If you started using Passport before v13, your oauth_clients table may not have a grant_types column yet. Add it first using the migration described in the Passport upgrade guide.

You are done!

Usage

Example of usage with axios:

axios.post('/oauth/token', {
    grant_type: 'social', // static 'social' value
    client_id: clientId, // client id
    client_secret: clientSecret, // client secret
    provider: providerName, // name of provider (e.g., 'facebook', 'google' etc.)
    access_token: providerAccessToken, // access token issued by specified provider
})
    .then((response) => {
        const {
            access_token: accessToken,
            expires_in: expiresIn,
            refresh_token: refreshToken,
        } = response.data;

        // success logic
    })
    .catch((error) => {
        const {
            message,
            hint,
        } = error.response.data;

        // error logic
    });

Example of usage with guzzlehttp/guzzle:

<?php

use GuzzleHttp\Client;
use Illuminate\Support\Arr;

$http = new Client;

$response = $http->post($domain . '/oauth/token', [
    RequestOptions::FORM_PARAMS => [
        'grant_type' => 'social', // static 'social' value
        'client_id' => $clientId, // client id
        'client_secret' => $clientSecret, // client secret
        'provider' => $providerName, // name of provider (e.g., 'facebook', 'google' etc.)
        'access_token' => $providerAccessToken, // access token issued by specified provider
    ],
    RequestOptions::HTTP_ERRORS => false,
]);
$data = json_decode($response->getBody()->getContents(), true);

if ($response->getStatusCode() === Response::HTTP_OK) {
    $accessToken = Arr::get($data, 'access_token');
    $expiresIn = Arr::get($data, 'expires_in');
    $refreshToken = Arr::get($data, 'refresh_token');

    // success logic
} else {
    $message = Arr::get($data, 'message');
    $hint = Arr::get($data, 'hint');

    // error logic
}

Testing

You can run the tests with:

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Upgrading from an older version? See UPGRADE.

Contributing

Please see CONTRIBUTING for details.

Credits

Created by Illia Sakovich

Maintained by Ankur Kumar

License

The MIT License (MIT). Please see License File for more information.

Related Packages

mikemclin/passport-custom-request-grant

Custom Request Grant for Laravel Passport

3,705 34
danjdewhurst/laravel-passport-facebook-login

Facebook Token Request Grant for Laravel Passport

24,475 28
mirkco/laravel-passport-facebook-login

Facebook Token Request Grant for Laravel Passport

498 8
adaojunior/passport-social-grant

Social grant for Laravel Passport

290,837 116
codingpassion360/laravel-social-passport

Easy to use Laravel-Passport framework for social authentication

148 2