1. Go to this page and download the library: Download eljam/guzzle-jwt-middleware 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/ */
eljam / guzzle-jwt-middleware example snippets
use Eljam\GuzzleJwt\JwtMiddleware;
use Eljam\GuzzleJwt\Manager\JwtManager;
use Eljam\GuzzleJwt\Strategy\Auth\QueryAuthStrategy;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
ull;
$baseUri = 'http://api.example.org/';
// Create authClient
$authClient = new Client(['base_uri' => $baseUri]);
//Create the JwtManager
$jwtManager = new JwtManager(
$authClient,
$authStrategy,
$persistenceStrategy,
[
'token_url' => '/api/token',
]
);
// Create a HandlerStack
$stack = HandlerStack::create();
// Add middleware
$stack->push(new JwtMiddleware($jwtManager));
$client = new Client(['handler' => $stack, 'base_uri' => $baseUri]);
try {
$response = $client->get('/api/ping');
echo($response->getBody());
} catch (TransferException $e) {
echo $e->getMessage();
}
//response
//{"data":"pong"}
use Eljam\GuzzleJwt\Persistence\SimpleCacheTokenPersistence;
use Psr\SimpleCache\CacheInterface;
/**
* @var CacheInterface
*/
$psr16cache;
$persistenceStrategy = new SimpleCacheTokenPersistence($psr16cache);
use Eljam\GuzzleJwt\Persistence\SimpleCacheTokenPersistence;
use Psr\SimpleCache\CacheInterface;
/**
* @var CacheInterface
*/
$psr16cache;
$ttl = 1800;
$cacheKey = 'myUniqueKey';
$persistenceStrategy = new SimpleCacheTokenPersistence($psr16cache, $ttl, $cacheKey);
namespace App\Jwt\Persistence;
use Eljam\GuzzleJwt\Persistence\TokenPersistenceInterface;
class MyCustomPersistence implements TokenPersistenceInterface
{
/**
* Save the token data.
*
* @param JwtToken $token
*/
public function saveToken(JwtToken $token)
{
// Use APCu, Redis or whatever fits your needs.
return;
}
/**
* Retrieve the token from storage and return it.
* Return null if nothing is stored.
*
* @return JwtToken Restored token
*/
public function restoreToken()
{
return null;
}
/**
* Delete the saved token data.
*/
public function deleteToken()
{
return;
}
/**
* Returns true if a token exists (although it may not be valid)
*
* @return bool
*/
public function hasToken()
{
return false;
}
}