<?php
require '../../../config/db.php';

header("Access-Control-Allow-Origin: * ");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../../../vendor/firebase/php-jwt/src/BeforeValidException.php';
include_once '../../../vendor/firebase/php-jwt/src/ExpiredException.php';
include_once '../../../vendor/firebase/php-jwt/src/SignatureInvalidException.php';
include_once '../../../vendor/firebase/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;

$postData = file_get_contents("php://input");

// $authHeader = $_SERVER;
// $arr = explode(" ", $authHeader);
$secretKey = "sA6HTfF3DVheBwpaXAgC2gDqDteYusWRn73t9AdbggtE9RAXzWhAnr8TPaSQp3CC";

if(isset($postData) && !empty($postData)){
    //Extract the data
    $request = json_decode($postData);
   

    //Validate data
    if(trim($request->refreshToken)===''){
        return http_response_code(400);
    }
			
    //Sanitize
    $refreshToken = mysqli_real_escape_string($con, trim($request->refreshToken));
    try {

        $decoded = JWT::decode($refreshToken, $secretKey, array('HS256'));

        // Access is granted. Add code of the operation here 
        $issuer_claim = "ayush"; // this can be the servername
        $audience_claim = "THE_AUDIENCE";
        $issuedat_claim = time(); // issued at
        $notbefore_claim = $issuedat_claim + 10; //not before in seconds
        $expire_claim = $issuedat_claim + (3600); // expire time in seconds
        $accessToken = array(
            "iss" => $issuer_claim,
            "aud" => $audience_claim,
            "iat" => $issuedat_claim,
            "nbf" => $notbefore_claim,
            "exp" => $expire_claim,
            "data" => array(
                "userId" => $decoded->data->userId,
                "fname" => $decoded->data->fname,
                "lname" => $decoded->data->lname,
                "phone" => $decoded->data->phone,
        ));
        $refreshToken = array(
            "iss" => $issuer_claim,
            "aud" => $audience_claim,
            "iat" => $issuedat_claim,
            "nbf" => $notbefore_claim,
            "exp" => $expire_claim+(3600),
            "data" => array(
                "userId" => $decoded->data->userId,
                "fname" => $decoded->data->fname,
                "lname" => $decoded->data->lname,
                "phone" => $decoded->data->phone,
        ));

        http_response_code(200);

        $accessJwt = JWT::encode($accessToken, $secretKey);
        $refreshJwt = JWT::encode($refreshToken, $secretKey);
        echo json_encode(
            array(
                "message" => "Successful login.",
                "accessToken" => $accessJwt,
                "refreshToken" => $refreshJwt,
                "expireAt" => $expire_claim
            )); 
    }catch (Exception $e){

        http_response_code(401);

        echo json_encode(array(
            "message" => "Access denied.",
            "error" => $e->getMessage()
        ));
    }

}

?>