I faced the issue I can’t solve right now. I have an error axios, the code is not so long, but I have no idea where exactly to look. So I just wondered maybe someone could help me out ? The problem is somewhere in the client code, because I tried to test it with POSTMAN then I get status OK 200, but when I try to log in from UI then I get axios error.
const url = BACKEND_URL;
console.log(`${url}/api_app`);
const logo = require("../../images/logo-pinigine.png");
const credentials = `${validUsername}:${validPassword}`;
const encodedCredentials = encode(credentials);
const instance = axios.create({
baseURL: `${url}/api_app`,
headers: {
Authorization: `Basic ${encodedCredentials}`,
Accept: "application/json",
},
});
const handleLogin = async () => {
if (isInputValid()) {
if (isHuman) {
try {
var returnValue = 0;
const res = await instance.post(`/app_actions.php`, {
action: "find_user_by_email",
email: email,
password: password,
});
const data = res.data;
if (data.success == 1) {
returnValue = data.success;
setRajonu_arr(data.data);
} else {
returnValue = data.success;
toaster.show({
message: data.msg,
type: "error",
position: "middle",
duration: 2000,
});
}
} catch (error) {
console.log(error);
}
if (returnValue !== 0) {
changeRajonaiModalVisible(true);
}
} else {
toaster.show({
message: "Pirma pažymėkite, kad nesate robotas.",
type: "error",
position: "middle",
duration: 2000,
});
}
}
};
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/System.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/functions.php');
$json = file_get_contents('php://input');
$data = json_decode(file_get_contents("php://input"));
//require_once($_SERVER['DOCUMENT_ROOT'] . '/ajax/helpers.php');
error_reporting(E_ALL);
// Set the username and password for authentication
global $mysqli;
//is bendrines weekly nustatymas kad ateitu
$validUsername = nustatymai_par_reiksme($mysqli, 'validApiUsername');
$validPassword = nustatymai_par_reiksme($mysqli, 'validApiPassword');
//print_r(" validUsername ".$validUsername." validPassword ".$validPassword);
//$validUsername = 'username';
//$validAPIUsername = 'password';
// Check if the Authorization header is set
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="API Authentication"');
exit;
}
// Verify the provided credentials
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
//$username = $_GET['PHP_AUTH_USER'];
//$password = $_GET['PHP_AUTH_PW'];
if ($username !== $validUsername || $password !== $validPassword && isset($data->system_id)) {
header('HTTP/1.1 401 Unauthorized');
exit;
}
// Authentication successful, handle API requests
if (isset($_POST['action'])) {
$action = $_POST['action'];
// Call the appropriate function based on the action
switch ($action) {
case 'find_user_by_email':
// Call function1
find_user_by_email();
break;
default:
// Invalid action, return an error response
$response = ['error' => 'Invalid action'];
echo json_encode($response);
break;
}
} else {
// No action specified, return an error response
$response = ['error' => 'No action specified'];
echo json_encode($response);
}
function find_user_by_email()
{
global $mssql, $mysqli;
$return = [];
$email = $_POST['email'];
$System = new System();
if (isset($email)) {
$systems_rajonai = $System->getSystemRajonaiList($email);
if (count($systems_rajonai) > 0) {
$return['msg'] = 'rajonai gauti';
$return['data'] = $systems_rajonai;
$return['success'] = 1;
} else {
$return['msg'] = 'Tokio vartotojo E-Maitinimas sistemoje nėra';
$return['success'] = 0;
}
} else {
$return['msg'] = 'neivestas email';
$return['success'] = 0;
}
echo json_encode($return);
}
?>
And I have this PHP file
I don’t know about php, this file was built for me
I expect to have status ok 200 from client side
I checked the other topics but wasn’t helpful, because there was the main problem that he should enable CORS in server side which I am already enabled also I am using credentials which is encoded and none of them was used. And I am using PHP differently than others, maybe there can be the issue aswell. And also I am using Authorization: Basic Auth, maybe I missed something there in client side.
Ugnius Uscilas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1