hamedov/passport-grants is a Laravel package for custom grants for laravel passport.
It currently has 3 GitHub stars and 145 downloads on Packagist (latest version 4.1.3).
Install it with composer require hamedov/passport-grants.
Discover more Laravel packages by hamedov
or browse all Laravel packages to compare alternatives.
Last updated
Add custom grants to laravel passport.
composer require hamedov/passport-grants
| Passport version | Passport grants version | |:----------------:|:-----------------------:| | ^7.0 | ^1.0 | | ^8.0 | ^2.0 | | ^9.0 | ^3.0 | | ^10.0 | ^4.0 |
We will be using facebook login as an example here
php artisan make:grant Facebook
This will create a new grant class in App\Grants folder
protected $identifier = 'facebook';
protected $authParams = [
'jwt_token',
];
The access token request should look like this:
$response = $http->post(env('APP_URL') . '/oauth/token', [
'form_params' => [
'grant_type' => 'facebook',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'jwt_token' => 'facebook access token',
'scope' => '',
'guard' => 'api',
],
]);
getUserEntityByAuthParams methodYou will receive an empty instance of the authenticated model as first parameter.
The second parameter is an associative array containing values of parameters specified in authParams property.
protected function getUserEntityByAuthParams(Model $model, $authParams,
$guard, $grantType, ClientEntityInterface $clientEntity)
{
// Do your logic to authenticate the user.
// Return false or void if authentication fails.
// This will throw OAuthServerException.
$facebookAccessToken = $authParams['jwt_token'];
// Contact facebook server to make sure the token is valid and get the corresponding user profile.
$profile = file_get_contents('https://graph.facebook.com/me?fields=name,email&access_token='.$facebookAccessToken);
$profile = (array) json_decode($profile);
if ( ! isset($profile['email'])) {
// We cannot identify the user without his email address
return;
}
// Retrieve user or any authenticatable model by email or create new one.
$user = $model->firstOrCreate(['email' => $profile['email']], ['name' => $profile['name']]);
return new User($user->getAuthIdentifier());
}
You can use the previous example with any authenticatable entity.
Add the new grant to grants section of config/auth.php
'grants' => [
App\Grants\Facebook::class,
],
Released under the Mit license, see LICENSE