composer require consilience/laravel-jwt
or more likely:
composer require consilience/laravel-jwt dev-master
Until released on packagist, include the repository in your composer.json:
"repositories": [
{
"type": "vcs",
"url": "git@github.com:consilience/laravel-jwt.git"
}
]$ php artisan vendor:publish --provider='Consilience\LaravelJwt\LaravelJwtServiceProvider'
Manually copy the config file:
cp vendor/consilience/laravel-jwt/config/jwt.php config/jwt.php
In bootstrap/app.php:
$app->configure('jwt');
In bootstrap/app.php:
$app->register(Consilience\LaravelJwt\LaravelJwtServiceProvider::class);
In bootstrap/app.php:
if (! class_exists('JwtService')) {
class_alias('Consilience\LaravelJwt\JwtServiceFacade', 'JwtService');
}If you're using certificates for encoding/decoding JWTs, you'll need to specify the path to those files.
JWT_SECRET=some-long-string
JWT_LEEWAY_SECONDS=600
JWT_ALGO=hs256
JWT_PUBLIC_KEY='storage/app/certs/somefile.cer'
JWT_PRIVATE_KEY='storage/app/certs/somefile.pem'
JWT_PASSPHRASE=some-long-string
First you'll want to import the class.
use Consilience\LaravelJwt\JwtService;Now create an instance of the JwtService to use.
$jwt = new JwtService();Now you have access to the following methods:
- parseToken() - Attempt to decode a token, returns the token's payload.
$payload = $jwt->parseToken($token);- createToken() - Create a new JWT from provided payload.
$payload = array(
"sub" => "1234567890",
"name" => "John Doe",
"iat" => 1516239022
);
$new_jwt = $jwt->createToken($payload);- validateToken() - Pass some built-in Laravel validation rules to validate the contents of a JWT.
$rules = [
'sub' => 'required|string|max:15',
'name' => 'required|string|max:20',
'iat' => 'required|numeric|max:10'
];
$success = $jwt->validateToken($token, $rules);