PHP code example of wirecore / cakephp_jwt

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

    

wirecore / cakephp_jwt example snippets


$this->addPlugin('Wirecore/CakePHP_JWT');

$this->loadComponent("Wirecore/CakePHP_JWT.Jwt");

$this->loadComponent("Wirecore/CakePHP_JWT.Jwt", [
    'tokenExpiration' => 900, // default is 900 seconds
    'headerParam' => 'Authorization', // default is Authorization
    'usersTable' => 'Users', // default is Users
    'unauthorizedExceptionText' => 'You are not authorized to access that location', // default is You are not authorized to access that location
    'encryptionKey' => '', // default is used the salt of your application
    'refreshTokenName' => 'refresh_token', // default is refresh_token
    'refreshTokenSecure' => false, // default is false
    'refreshTokenHttpOnly' => true, // default is true
    'hostAddPort' => false // if by generation the refresh token server path is not available, it used the host server variable. by enabling this option it add the current available port to the host 
]);

$this->Jwt->allowUnauthenticated(['index']);

$this->Jwt->getIdentity();

$this->Jwt->generateAccessToken($userId);

$this->Jwt->generateRefreshToken($userId);

$this->Jwt->setRefreshTokenCookie($userId);

$this->Jwt->refreshTokens();

public function initialize():void{
    parent::initialize();
    $this->Jwt->allowUnauthenticated(['login', 'refreshToken']);
}

public function login(){

    $response = $this->getResponse();
    $data = $this->request->getData();

    // <-- checking user password here

    $userId = 123; // for exmaple here is userId 123

    // password correct
    $token = $this->Jwt->generateAccessToken($userId); // access token for 15 minute authentication
    $this->Jwt->setRefreshTokenCookie($userId); // refresh token for refreshing the access token

    $response = $response->withStatus(200);

    $this->set('token', $token);
    $this->viewBuilder()->setOption('serialize', 'token');
    $this->viewBuilder()->setClassName('Json');
    $this->setResponse($response);

}

public function refreshToken(){

    $response = $this->getResponse();

    // <-- checking user password here

    $token = $this->Jwt->refreshTokens(); // generate a new access token for 15 minutes and actualize the refresh token cookie

    $response = $response->withStatus(200);

    $this->set('token', $token);
    $this->viewBuilder()->setOption('serialize', 'token');
    $this->viewBuilder()->setClassName('Json');
    $this->setResponse($response);

}

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "Wirecore\\CakePHP_JWT\\": "plugins/Wirecore/CakePHP_JWT/src/",
        "Wirecore\\CakePHP_JWT\\Test\\": "plugins/Wirecore/CakePHP_JWT/tests/"
    }
},

composer dumpautoload