PHP code example of altelma / php-jwt

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

    

altelma / php-jwt example snippets


'providers' => [
    Altelma\JWT\JWTServiceProvider::class,
],

$app->register(Altelma\JWT\JWTServiceProvider::class);

$app->configure('jwt');

$app->withFacades();

if (!class_exists('JWT')) {
    class_alias('Altelma\JWT\JWTFacade', 'JWT');
}



namespace App\Http\Controllers;

use GuzzleHttp\Client;

class JwtController extends Controller
{
    private function getJWT()
    {
        $client = new Client();
        $response = $client->post('your_auth_url', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'email' => '[email protected]',
                'password' => 'password'
            ]
        ]);

        $response = json_decode($response->getBody(), true);

        return $response['accesstoken'];
    }

    public function verifyJwt()
    {
        $jwtToken = 'YOUR_JWT_TOKEN';
        $verifyToken = \JWT::verify('sha256', $jwtToken);

        return ['success' => $verifyToken];
    }

    public function genJwt()
    {
        $header = [
            "alg"     => "RS256",
            "typ"     => "JWT"
        ];

        $payload = [
            "sub"        => "465465464646",
            "name"        => "John Doe",
            "admin"        => true
        ];

        return ['success' => true, 'access_token' => \JWT::generate('sha256', $header, $payload)];
    }
}