<?php header("Access-Control-Allow-Origin: *");
  header("Access-Control-Allow-Headers: *");  

  header('Content-Type: application/json');

  include '../db/connectMe.php';   
  include '../function/function.php';  

  // (C) GENERATE JWT TOKEN
  require "../vendor/autoload.php";
  use Firebase\JWT\JWT;


  if( AuthorizeMe(@substr(getallheaders()["Authorization"], 7)) ){

    $jwt = substr(getallheaders()["Authorization"], 7);

    //Decoding data
    $decodeToken = (json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $jwt)[1]))))->data);

    if(!isset($decodeToken)){

      exit(json_encode(array("error" => "unauthorised") ));

    }

    
    //All users

    $userSet = find_all_users(1); 

    $filteredUsers = []; // Array to store users without passwords

    foreach ($userSet as $user) {  
      $userArray = (array) $user; // Ensure it's an array

      unset($userArray['tbl_userPass']); // Remove password field

      $filteredUsers[] = $userArray; // Add modified user to new array
    }


    exit(json_encode($filteredUsers));

  }else{

    exit(json_encode(array("error" => "unauthorised") ));

  }