Calling Global Functions in API Actions

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
rembo
User
Posts: 227

Calling Global Functions in API Actions

Post by rembo »

Hello
I tried to called all of this functions from API server events side in "userfn.php" file --> "CurrentUserName()","CurrentUserID()","isAdmin()","isLogedin()"
But not either of this function work with me , Not return any response in LOG file .
Any idea ?
Thanx ..


mobhar
User
Posts: 11700

Post by mobhar »

From which server event did you use that global functions? Please always post your code for more discussion.

In addition, there are no isAdmin() or isLogedin() global functions. It should be IsAdmin() or IsLoggedIn() respectively.


rembo
User
Posts: 227

Post by rembo »

  //Generating Promo Code API (for Admin's Only)
   $app->post('/genPromo[/{params:.*}]', function ($request, $response, array $args) {
      $list_id = $request->getParsedBodyParam('list_id');
      $start_today = $request->getParsedBodyParam('start_today');
      $signture =  $request->getParsedBodyParam('signture');
      Log("Promo Generated List CallBack----------------->".$list_id);
      if($list_id !== null && $signture !== null){
          Log("Promo Generated ADMIN  signture List CallBack----------------->".IsAdmin());
          Log("Promo Generated Encrypct signture List CallBack----------------->".$signture);
          $start_gen_promo = generatePromoCodesAction($list_id,$signture,$start_today);
    Log("Promo Generated List CallBack----------------->".$start_gen_promo);
          if($start_gen_promo){
             return $response->withJson(["success" => true]); 
        Log("Promo Generated List sucssess CallBack----------------->".$list_id);
          }else{
             return $response->withJson(["success" => false]); 
          }
      }
      return $response->withJson(["success" => false]);
   });

here the loger response for IsAdmin() fun :

[2022-08-01T14:14:39.427447+00:00] log.DEBUG: Promo Generated ADMIN  signture List CallBack-----------------> [] []

I still get empty response from API


mobhar
User
Posts: 11700

Post by mobhar »

mobhar wrote:

From which server event did you use that global functions?


rembo
User
Posts: 227

Post by rembo »

SeverEvents -> API-Action


arbei
User
Posts: 9347

Post by arbei »

If your API actions requires security, you need to try login the user first, you may refer to source code of PermissionMiddleware.


rembo
User
Posts: 227

Post by rembo »

Thanx ,it's work , I need to sign the user in script part before I call the API action..
I think that I have made same mistake before ,
is there a way that I Can use this action as single action inside the promo list without using API action in server events ?


arbei
User
Posts: 9347

Post by arbei »

It depends what you are trying to do, you may also read Ajax by API and Client Scripts.


rembo
User
Posts: 227

Post by rembo »

I have tried use this CallBack function to the script part using "xhr" post method to Authenticate User before posting, But I still got an empty response from security function "IsAdmin()"

[2022-08-05T11:11:49.250231+00:00] log.DEBUG: Promo Generated ADMIN  signture List CallBack-----------------> [] []

here is the script part :

<script>
loadjs.ready("load", function () {
  // Write your table-specific startup script here, no need to add script tags.
    var store = store || {};
    // Store JWT
    store.setJWT = function(data) {
        this.JWT = data;
    }
 window.mySuccessCallbackNoDate = function() {
        Swal.fire({
            title: '<?= $Language->Phrase("openegbaner")?>',
            html: '<?= $Language->Phrase("plesewait")?>',
            showConfirmButton: false
        }); 
        var data_set = '<?= $Page->list_id->QueryStringValue ?>' ;
        var signture_set = '<?= encrypt_decrypt_mobile_sign(Config("SECRET_SIGNTURE_PROMO_GENERATER_KEY_FIELD_NAME"),'encrypt') ?>' ;
        var logindata = {username:"admin" , password:"1234"};
        $.post("https://www.server.com/myProject/api/login", logindata, function(logindata) {
               store.setJWT(logindata.JWT);
        }).fail(function(xhr, status, error) {
            alert("login failed. status: " + status + ", error: " + error);
        }).always(function(){
            // WARNING: For POST requests, body is set to null by browsers.
            console.log(store.JWT);
            var datatoPost = "list_id=" + data_set + "&signture=" + signture_set;
            var xhr = new XMLHttpRequest();
            xhr.withCredentials = true;

            xhr.addEventListener("readystatechange", function() {
                if(this.readyState === 4) {
                    console.log(this.responseText);
                }
            });

            xhr.open("POST", "https://www.server.com/myProject/api/genPromo");
            xhr.setRequestHeader("X-Authorization", "Bearer " + store.JWT);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            // WARNING: Cookies will be stripped away by the browser before sending the request.
            xhr.send(datatoPost);
        });
    };
});
</script>

any idea , Highly thanks for your kindness


Post Reply