PHP code example of marcohern / jwtauthorize

1. Go to this page and download the library: Download marcohern/jwtauthorize 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/ */

    

marcohern / jwtauthorize example snippets


[
    'allow * /.*/' => [
        'deny * /\/admin(\/.*)?/' => [
            'allow GET /\/admin\/reports/',
        ],
    ],
]

Route::middleware(['auth:api', 'jwta'])->group(function () {
    Route::get('/posts', [PostController::class, 'index']);
});

$token = auth('api')
    ->claims(['scope' => ['allow GET /.*/', 'deny * /\/admin(\/.*)?/']])
    ->attempt($credentials);

use Marcohern\Jwtauthorize\PolicyManager;

$token = auth('api')
    ->claims(['scope' => app(PolicyManager::class)->claim('reader', 'editor')])
    ->attempt($credentials);

// AppServiceProvider::boot()
Gate::define('jwtauthorize.manage', fn (User $user) => $user->is_admin);
bash
php artisan vendor:publish --tag="jwtauthorize-config"
text
allow * /.*/                     allow everything
deny POST /\/admin(\/.*)?/       deny POST to /admin and /admin/...
allow GET,HEAD /\/users\/\d+/    allow reading /users/{id}
bash
php artisan jwta:role:create reader "allow GET /.*/"
php artisan jwta:role:create editor --file=editor.json   # nested policies, same shape as above
php artisan jwta:role:update reader "allow GET,POST /.*/"
php artisan jwta:role:list
php artisan jwta:role:show editor
php artisan jwta:role:delete reader
bash
php artisan jwta:role:test editor GET /admin/reports
text
ALLOWED  GET /admin/reports  (role: editor)

✓ ▶ deny * /\/admin(\/.*)?/
✓ ▶   allow GET /\/admin\/reports/  ← decides
·     allow GET /.*/
bash
php artisan vendor:publish --tag="jwtauthorize-views"