a3naumov / laravel-jwt
JWT support for Laravel
Requires
- php: ^8.5
- ext-openssl: *
- ext-sodium: *
- laravel/framework: ^13.0
- lcobucci/jwt: ^5.7
- psr/clock: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95.32
- orchestra/testbench: ^11.0
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.
Requirements
- PHP
^8.5with 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.
-
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.pemandpublic.peminstorage/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. -
Register the guard and user provider. Add the JWT guard to
guardsinconfig/auth.php.'jwt' => ['driver' => 'jwt'],
Add the example
CustomerandCustomerAuthenticatableProviderclasses toapp/Auth. Complete provider exampleBind the provider in
AppServiceProvider::register().use A3Naumov\JwtAuth\Laravel\AuthenticatableProvider; use App\Auth\CustomerAuthenticatableProvider; // Inside AppServiceProvider::register() $this->app->bind(AuthenticatableProvider::class, CustomerAuthenticatableProvider::class);
-
Log in and get an access token. Set
routes.login.enabledtotrueinconfig/jwt-auth.php.'routes' => [ 'login' => [ 'enabled' => true, ], ],
Run
php artisan servein 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/loginExpect HTTP 200 with a JSON body like this. Copy the value of
access_token.{ "access_token": "<signed JWT>", "token_type": "Bearer", "expires_in": 900 } -
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
- Enable token rotation and logout with refresh sessions.
- Issue tokens from your own login use case with custom login and DDD integration.
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.