PHP code example of bluemill / jwt-artisan

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

    

bluemill / jwt-artisan example snippets


// Laravel
// config/app.php
'providers' => [
    ...
    GenTux\Jwt\Support\LaravelServiceProvider::class,
]

// Lumen
// bootstrap/app.php
$app->register(GenTux\Jwt\Support\LumenServiceProvider::class);



use GenTux\Jwt\JwtToken;

class TokensController extends controller
{
    public function create(JwtToken $jwt)
    {
        $payload = ['exp' => time() + 7200]; // expire in 2 hours
        $token = $jwt->createToken($payload); // new instance of JwtToken

        return (string) $token;
    }
}



use GenTux\Jwt\JwtPayloadInterface;

class User extends Model implements JwtPayloadInterface
{
    public function getPayload()
    {
        return [
            'sub' => $this->id,
            'exp' => time() + 7200,
            'context' => [
                'email' => $this->email
            ]
        ];
    }
}



use GenTux\Jwt\JwtToken;

class TokensController extends controller
{
    public function create(JwtToken $jwt)
    {
        $user = User::find(1);
        $token = $jwt->createToken($user);

        return $token->payload(); // ['sub' => 1, exp => '...', 'context' => ...]
    }
}

public function create(JwtToken $jwt)
{
    return $jwt
            ->setSecret('secret_123')
            ->setAlgorithm('custom')
            ->createToken('[...]');
}



// Laravel
Route::group(['middleware' => 'jwt'], function() {
    Route::post('/foo', 'FooController');
});

// Lumen
$app->group(['middleware' => 'jwt', 'namespace' => 'App\Http\Controllers'], function($app)  {
    $app->post('/foo', 'FooController');
});



use GenTux\Jwt\GetsJwtToken;

class CreateUser extends FormRequest
{
    use GetsJwtToken;

    public function authorize()
    {
        return $this->jwtToken()->validate();
    }
}



use GenTux\Jwt\GetsJwtController;

class FooController extends controller
{
    use GetsJwtToken;

    public function store()
    {
        if( ! $this->jwtToken()->validate()) {
            return redirect('/nope');
        }

        ...
    }
}



use GenTux\Jwt\GetsJwtToken;

class TokenService
{

    use GetsJwtToken;

    public function getExpires()
    {
        $payload = $this->jwtPayload(); // shortcut for $this->jwtToken()->payload()

        return $payload['exp'];
    }
}



use GenTux\Jwt\GetsJwtToken;

class TokenService
{
    use GetsJwtToken;

    public function getData()
    {
        // ['exp' => '123', 'context' => ['foo' => 'bar']]

        $token = $this->jwtToken();
        $token->payload('exp'); // 123
        $token->payload('context.foo'); // bar
        $token->payload('context.baz'); // null
    }
}



use GenTux\Jwt\Exceptions\JwtException;
use GenTux\Jwt\Exceptions\JwtExceptionHandler;

class Handler extends ExceptionHandler
{
    use JwtExceptionHandler;

    public function render($request, Exception $e)
    {
        if($e instanceof JwtException) return $this->handleJwtException($e);

        ...
    }
}