1. Go to this page and download the library: Download soluble/wallit 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/ */
soluble / wallit example snippets
use Zend\ConfigAggregator\ArrayProvider;
use Zend\ConfigAggregator\ConfigAggregator;
use Zend\ConfigAggregator\PhpFileProvider;
$cacheConfig = [
'config_cache_path' => 'data/config-cache.php',
];
$aggregator = new ConfigAggregator([
new ArrayProvider($cacheConfig),
// Register the Soluble Wallit ConfigProvider
Soluble\Wallit\Config\ConfigProvider::class,
new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
new PhpFileProvider('config/development.config.php'),
], $cacheConfig['config_cache_path']);
return $aggregator->getMergedConfig();
declare(strict_types=1);
namespace App\Handler;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\RequestHandlerInterface;
use Fig\Http\Message\StatusCodeInterface;
use Ramsey\Uuid\Uuid;
use Soluble\Wallit\Service\JwtService;
use Soluble\Wallit\Token\Jwt\JwtClaims;
use Zend\Diactoros\Response\JsonResponse;
class AuthHandler implements RequestHandlerInterface
{
/**
* @var JwtService
*/
protected $jwtService;
public function __construct(JwtService $jwtService)
{
$this->jwtService = $jwtService;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$method = $request->getMethod();
if ($method !== 'POST') {
throw new \RuntimeException('TODO - Handle error your way ;)');
}
$body = $request->getParsedBody();
$login = $body['login'] ?? '';
$password = $body['password'] ?? '';
if ($login === 'demo' && $password === 'demo') {
$token = $this->jwtService->createToken([
JwtClaims::ID => Uuid::uuid1(),
'login' => $login
]);
return new JsonResponse([
'access_token' => (string) $token,
'token_type' => 'example',
]);
}
return (new JsonResponse([
'success' => false
]))->withStatus(StatusCodeInterface::STATUS_UNAUTHORIZED);
}
}
declare(strict_types=1);
namespace App\Handler;
use Psr\Container\ContainerInterface;
use Soluble\Wallit\Service\JwtService;
class AuthHandlerFactory
{
public function __invoke(ContainerInterface $container): AuthHandler
{
return new AuthHandler(
$container->get(JwtService::class)
);
}
}