PHP code example of corbosman / laravel-passport-claims

1. Go to this page and download the library: Download corbosman/laravel-passport-claims 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/ */

    

corbosman / laravel-passport-claims example snippets



namespace App\Claims;
use CorBosman\Passport\AccessToken;  // extends Laravel\Passport\Bridge\AccessToken

class CustomClaim
{
    public function handle(AccessToken $token, $next)
    {
        $token->addClaim('my-claim', 'my custom claim data');

        return $next($token);
    }
}



namespace App\Claims;

use App\User;
use CorBosman\Passport\AccessToken; // extends Laravel\Passport\Bridge\AccessToken

class CustomClaim
{
    public function handle(AccessToken $token, $next)
    {
        $user = User::find($token->getUserIdentifier());

        $token->addClaim('email', $user->email);

        return $next($token);
    }
}



return [
    /*
    |--------------------------------------------------------------------------
    | JWT Claim Classes
    |--------------------------------------------------------------------------
    |
    | Here you can add an array of classes that will each be called to add
    | claims to the passport JWT token. See the readme for the interface that
    | these classes should adhere to.
    |
    */
    'claims' => [
        App\Claims\MyCustomClaim::class,
        App\Claims\MyOtherCustomClaim::class
    ]
];

    protected $routeMiddleware = [
        'claim' => \CorBosman\Passport\Http\Middleware\CheckForClaim::class,
    ];

Route::middleware(['client', 'claim:my-claim'])->get('my-protected-route', function () {
    return 'protected by claim';
});

 Route::middleware(['client', 'claim:my-claim,foobar'])->get('my-protected-route', function () {
     return 'protected by claim with foobar as its value';
 });
 

 Route::middleware(['client', 'claim:my-claim,foo|bar'])->get('my-protected-route', function () {
     return 'protected by claim with foo or bar as its value';
 });
 

   'formatters' => [
        \Lcobucci\JWT\Encoding\UnifyAudience::class,
        \Lcobucci\JWT\Encoding\UnixTimestampDates::class,
    ]
bash
php artisan claim:generate Claims/CustomClaim
shell
php artisan vendor:publish --provider="CorBosman\Passport\ServiceProvider"