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 ..
Calling Global Functions in API Actions
-
- User
- Posts: 226
Calling Global Functions in API Actions
-
- User
- Posts: 226
//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
-
- User
- Posts: 9438
It depends what you are trying to do, you may also read Ajax by API and Client Scripts.
-
- User
- Posts: 226
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