PHP code example of apigopro / slim-jwt-auth

1. Go to this page and download the library: Download apigopro/slim-jwt-auth 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/ */

    

apigopro / slim-jwt-auth example snippets


use SlimJwtAuth\JwtAuthMiddleware;

$app->add(new JwtAuthMiddleware([
    'secret' => 'supersecretkeyyoushouldnotcommittogithub',
    'path'   => ['/api'],          // only these paths dClass)
    $response->getBody()->write($claims->sub);
    return $response;
});

new JwtAuthMiddleware([
    'secret' => [
        '2026-01' => 'old-key-still-valid-for-existing-tokens',
        '2026-07' => 'current-signing-key',
    ],
    'algorithm' => 'HS256',
]);

use SlimJwtAuth\JwtAuthMiddleware;
use SlimJwtAuth\RbacMiddleware;

$app->add(new JwtAuthMiddleware(['secret' => $secret, 'path' => ['/api']]));

// Only "admin" (or any role you list) may hit this group.
$app->group('/api/admin', function ($group) {
    $group->get('/users', ListUsersAction::class);
})->add(RbacMiddleware::

new RbacMiddleware([
    'permissions' => ['posts.delete'],
    'role_permissions' => [
        'admin'  => ['*'],                         // "*" grants every permission
        'editor' => ['posts.create', 'posts.edit'],
    ],
]);

RbacMiddleware:::