lgrevelink / laravel-simple-jwt
Utilties for using the php-simple-jwt package in Laravel/Lumen projects.
Installs: 1 819
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.1
- illuminate/console: >=5.7
- illuminate/support: >=5.7
- illuminate/validation: >=5.7
- lgrevelink/php-simple-jwt: ^0.2
Requires (Dev)
- laravel/framework: >=5.7
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-11-09 20:01:53 UTC
README
Laravel and Lumen utilities to kickstart the usage of the PHP Simple JWT package.
Supported functionalities;
- Artisan make command for JWT Blueprints (
make:jwt-blueprint
) - Validator rule for JWT validation
Installation
composer require lgrevelink/laravel-simple-jwt
For Laravel
Laravel's auto-discovery directly registers the service provider so it should be instantly usable. If you don't use auto-discovery, please add the SimpleJwtServiceProvider
to the provider array in config/app.php
.
LGrevelink\LaravelSimpleJWT\Providers\SimpleJwtServiceProvider::class
Both the artisan command and the validator are directly registered by adding the service provider.
For Lumen
Using this package in lumen requires you to register the service provider in bootstrap/app.php
.
$app->register(LGrevelink\LaravelSimpleJWT\Providers\SimpleJwtServiceProvider::class);
Auto-extending the validator rule only works when the Facades have been setup. This can be done by enabling the withFacades
option in bootstrap/app.php
. Aliases are not necessary.
$app->withFacades();
Usage
Generator command
After getting set up, generating your first Blueprint is very easy.
php artisan make:jwt-blueprint MyFirstBlueprint
A series of questions are following setting up the default claims supported by JWT. Values you don't want to use can be left empty and won't be added. The default namespace where the blueprints will be placed is App\JwtTokens
.
All date-related claims (e.g. expiration time) are treated as relative values from the moment generation. Find more information about the Simple JWT package and its usage here.
Validator
There are 2 general ways of using the validator rule.
Basic usage
$request->validate([ 'foo' => 'jwt', 'bar' => new ValidJwt(), ]);
This only validates whether the token has a valid JWT structure.
Blueprint usage
$request->validate([ 'foo' => 'jwt:' . App\JwtTokens\MyFirstBlueprint::class, 'bar' => new ValidJwt(App\JwtTokens\MyFirstBlueprint::class), ]);
This validates the token directly against the given blueprint.
Tests
Tests are written with PHPUnit and can be run via the following composer command;
composer run test