PHPackages                             dbfun/jwtapi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. dbfun/jwtapi

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

dbfun/jwtapi
============

Obtaining Jwt tokens via API by login and password

040PHP

Since Jan 16Pushed 5y ago1 watchersCompare

[ Source](https://github.com/dbfun/jwtapi)[ Packagist](https://packagist.org/packages/dbfun/jwtapi)[ RSS](/packages/dbfun-jwtapi/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Introduction
============

[](#introduction)

Obtaining Jwt tokens from Laravel Passport via API by login and password. No `client_id` and `client_secret` required.

Token claims customization. No Guzzle dependency.

Installation
------------

[](#installation)

```
composer require dbfun/jwtapi
# Choose "yes":
php artisan passport:install --uuids
```

Save Client ID for future reference.

```
Personal access client created successfully.
Client ID: ..............
Client secret: ..............

```

`config/auth.php`:

```
'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    'hash' => false,
],
```

Add to `config/app.php`:

```
/*
 * Package Service Providers...
 */

Dbfun\JwtApi\JwtApiServiceProvider::class,
```

Add to `app/Providers/AuthServiceProvider.php`:

```
\Dbfun\JwtApi\JwtApi::routes();
// \Laravel\Passport\Passport::tokensCan(/* List of scopes */);
```

Add to model file `User.php`:

```
use Dbfun\JwtApi\Traits\JwtApiTokenTrait;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use JwtApiTokenTrait;
    use HasApiTokens;

    public function scopes(): array
    {
        // put here the logic
        return [];
    }
}
```

### Keys

[](#keys)

You can generate private and public keys via this snippet:

```
mkdir /tmp/keys
ssh-keygen -t rsa -f /tmp/keys/key
openssl rsa -in /tmp/keys/key -pubout > /tmp/keys/key.pub
cat /tmp/keys/*
```

Add to `.env`:

```
PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\n(private key)\n-----END RSA PRIVATE KEY-----"
PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\n(public key)\n-----END PUBLIC KEY-----"
PASSPORT_PERSONAL_ACCESS_CLIENT_ID=

```

Use personal Client ID from above. Or look in the `oauth_clients` table. Or generate new:

```
php artisan passport:client --personal
```

Auth providers
--------------

[](#auth-providers)

The auth provider is configured through configuration `auth.guards.api.provider`:

```
return [
    'guards' => [
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',                //  false,
        ],
    ],
    'providers' => [
        'users' => [                              //  'eloquent',               //  \App\Models\User::class,   //
