Search by

a3naumov / laravel-jwt

a3naumov

JWT support for Laravel

Package info

github.com/a3naumov/laravel-jwt

pkg:composer/a3naumov/laravel-jwt

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-12-03 13:20 UTC

This package is auto-updated.

Last update: 2026-12-03 15:15:42 UTC


README

JWT authentication for Laravel 13 with rotating refresh tokens, HttpOnly cookies, and support for your own domain entities and user repositories.

CI Coverage PHPStan level 10

Requirements

  • PHP ^8.5 with the OpenSSL and Sodium extensions enabled.
  • Laravel ^13.0.
  • Composer 2 with runtime API ^2.2.
  • A Laravel database connection with the corresponding PDO driver when refresh sessions are enabled.

Quick start

This example uses the default RS256 signing algorithm and an existing account in Laravel's users table.

  1. Install the package and configure signing. Run these commands from your Laravel app.

    composer require a3naumov/laravel-jwt
    php artisan vendor:publish --tag=jwt-auth-config
    php artisan jwt-auth:generate-keypair

    The command creates private.pem and public.pem in storage/app/private/jwt. The package uses the private key to sign access tokens and the public key to verify them. See Access tokens and signing for custom paths, encrypted keys, and HS256 setup.

  2. Register the guard and user provider. Add the JWT guard to guards in config/auth.php.

    'jwt' => ['driver' => 'jwt'],

    Add the example Customer and CustomerAuthenticatableProvider classes to app/Auth. Complete provider example

    Bind the provider in AppServiceProvider::register().

    use A3Naumov\JwtAuth\Laravel\AuthenticatableProvider;
    use App\Auth\CustomerAuthenticatableProvider;
    
    // Inside AppServiceProvider::register()
    $this->app->bind(AuthenticatableProvider::class, CustomerAuthenticatableProvider::class);
  3. Log in and get an access token. Set routes.login.enabled to true in config/jwt-auth.php.

    'routes' => [
        'login' => [
            'enabled' => true,
        ],
    ],

    Run php artisan serve in a separate terminal. Replace the email and password below with the credentials for your local account.

    curl -i --json '{"email":"customer@example.test","password":"your-password"}' \
      http://127.0.0.1:8000/api/auth/login

    Expect HTTP 200 with a JSON body like this. Copy the value of access_token.

    {
      "access_token": "<signed JWT>",
      "token_type": "Bearer",
      "expires_in": 900
    }
  4. Call a protected route. Add this route to routes/web.php.

    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Route;
    
    Route::get('/api/me', static fn (Request $request) => [
        'id' => $request->user()?->getAuthIdentifier(),
    ])->middleware('auth:jwt');

    Replace <access_token> with the value from the login response.

    ACCESS_TOKEN='<access_token>'
    curl -i --header "Authorization: Bearer ${ACCESS_TOKEN}" \
      --header 'Accept: application/json' \
      http://127.0.0.1:8000/api/me

    Expect HTTP 200 with your account's identifier, for example {"id":"1"}.

Next steps

Documentation

Guide What it covers
Configuration reference Every setting in config/jwt-auth.php, its default, and accepted values
Access tokens and signing Laravel guard, token lifetime, issuer, audience, and signing keys
Login and route configuration Login fields, response field names, route paths, middleware, and rate limits
Refresh sessions and logout JSON refresh, token rotation, session expiry, and revocation
HttpOnly refresh cookies Cookie settings, trusted origins, and CORS
Custom login and DDD integration Issuing tokens from your own use case and adapting your user provider
Running tests PHPUnit, Testbench, and SQLite

Testing

Run PHPUnit from the package root.

composer install
composer test

The functional tests boot Laravel through Testbench and use SQLite. The full suite, including concurrent refresh rotation, runs with the pdo_sqlite extension and default settings. See the testing guide.

License

This package is released under the MIT License.