JWT(JSON Web Token) (Sixth Token) Creation with CURL and Expire Time in CodeIgniter 3

 <?php

defined("BASEPATH") or exit("No direct script access allowed");

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class RequestSix extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $key = "Sixth_Key";
        //Set the expiration time to 1 minute from the current time
        $issuedAt = time();
        $expirationTime = $issuedAt + 60;     //jwt valid for 1 minute from the issued time
        $payload = [
            "id" => 31,
            "name" => "Patel Chandresh Rambhai",
            "email" => "patelchandresh7666@gmail.com",
            "nationality" => "Indian",
            "address" => "Nikol, Ahmedabad",
            "favourite game" => "Cricket",
            "Son Name" => "Prakash",
            "Roll No" => 22,
            "School Name" => "Devashya International School",
            "School Address" => "Nikol, Ahmedabad",
            "city" => "Ahmedabad",
            "pincode" => 352113,
            "iat" => $issuedAt,         //Issued at: time when the token was generated
            "nbf" => $issuedAt,         //Not before
            "exp" => $expirationTime    //Expiration time
        ];

        $jwt = JWT::encode($payload, $key, "HS256");

        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => 'http://localhost/blogApi/ApiSix/users',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_HTTPHEADER => [
                'Accept: application/json',
                'Authorization: Bearer ' . $jwt,
            ],
        ]);

        $response = curl_exec($curl);

        curl_close($curl);

        print_r($response);
    }
}







DECODE Data File
<?php
defined("BASEPATH") or exit("No direct script access allowed");

use chriskacerguis\RestServer\RestController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

class ApiSix extends RestController
{
    public function __construct()
    {
        parent::__construct();
    }
    public function users_post()
    {
        $headers = apache_request_headers();

        $jwt = $headers["Authorization"];

        $jwt = str_replace("Bearer", "", $jwt);
        $jwt = trim($jwt);
        print_r($jwt);
        echo "<br><br><br>";

        try {
            $decoded = JWT::decode($jwt, new Key("Sixth_Key", "HS256"));
            echo json_encode(array(
                "status" => "success",
                "data" => $decoded
            ));
            echo "<br><br><br>";
            echo "<pre>";
            $this->response($decoded);
        } catch (\Firebase\JWT\ExpiredException $e) {
            echo json_encode(array(
                "status" => "error",
                "message" => "Token is Expired"
            ));
        } catch (Exception $e) {
            echo json_encode(array(
                "status" => "error",
                "message" => $e->getMessage()
            ));
            print_r($e->getMessage());
            exit();
        }
    }
}

Comments