PHP code example of stechstudio / laravel-jwt
1. Go to this page and download the library: Download stechstudio/laravel-jwt library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
stechstudio / laravel-jwt example snippets
composer
$jwt = JWT::get('token-id', ['myclaim' => 'somevalue']);
$jwt = JWT::get('token-id', ['anything' => 'here'], 3600);
$jwt = JWT::get('token-id', ['anything' => 'here'], now()->addMinutes(60));
$jwt = JWT::get('token-id', ['anything' => 'here'], 3600, config('services.otherapp.key'));
$token = JWT::identifiedBy('my-token-id')
->lifetime(3600)
->signWith('custom-signing-key-with-256-bits')
->issuedBy("my-app")
->permittedFor("receiving-app")
->withClaim('myclaim', 'any value')
->getToken()
->toString();
$token = JWT::parse("... JWT string ...");
$token = JWT::parse("... JWT string ...");
$token->isValid('expected-token-id'); // Returns true or false
$token->validate('expected-token-id'); // Throws exceptions for any validation failure
// Make our string token
$jwt = JWT::get('token-id', ['foo' => 'bar']);
// Parse it and validate
$token = JWT::parse($jwt)->validate('token-id');
// Ignore registered claims, just get our custom claims
$token->getPayload(); // [ foo => bar ]
$token->get("foo"); // bar
$token->get("invalid"); // null
$token->get("invalid", "quz"); // quz
Route::get('/home', [Controller::class, 'home'])->name('my.home')->middleware('jwt');
Route::get('/home', [Controller::class, 'home'])->middleware('jwt:expected-id');
use Illuminate\Http\Request;
class Controller {
public function home(Request $request)
{
echo $request->getClaim('aud'); // The token audience
}
}