Код: Выделить всё
const HOST = "localhost";
const PORT = "8069";
const DB = "db_2345";
const webAuth = async () => {
try {
// Retrieve user credentials
const userData = await AsyncStorage.getItem('userData');
if (!userData) throw new Error('User not logged in');
const {user, pass} = JSON.parse(userData);
// Authenticate user
const authData = JSON.stringify({
jsonrpc: '2.0',
params: {
db: DB,
login: user,
password: pass,
},
});
const authRes = await fetch(
`http://${HOST}:${PORT}/web/session/authenticate`,
{
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials': true,
},
body: authData,
},
);
if (!authRes.ok) throw new Error('Authentication failed');
const sessionCookie = authRes.headers.get('set-cookie');
if (!sessionCookie)
throw new Error(
"Session ID couldn't be used! Unable to find it in headers",
);
return sessionCookie;
} catch (error) {
console.log(error);
throw error;
}
};
async function checkInOutSystem (latitude=20 , longitude=20) {
try{
const sessionCookie = await webAuth();
// Perform check-in/out
const locData = JSON.stringify({
jsonrpc: "2.0",
method: "call",
params: { latitude, longitude },
});
const attendanRes = await axios.post(
`http://${HOST}:${PORT}/hr_attendance/systray_check_in_out`,
locData,
{
// withCredentials: true,
headers: {
"Content-Type": "application/json",
Cookie: sessionCookie,
"Accept": "application/json",
},
}
);
console.log("attendanceRes:",attendanRes);
if (attendanRes.status !== 200) throw new Error("Check-in/out request failed");
console.log(attendanRes.data)
const responseJson = attendanRes.data;
console.log("responseJson",responseJson);
return responseJson;
} catch (error) {
console.error("Check-in/out error:", error.message);
throw error;
}
};
Любая помощь или информация об этом ценятся! Спасибо!
Подробнее здесь: https://stackoverflow.com/questions/794 ... unreliable