Refactor cookie handling in Index and update API call in Schedule for improved error logging and response handling
This commit is contained in:
@@ -56,7 +56,19 @@ export default function Index() {
|
||||
}
|
||||
);
|
||||
|
||||
const cookie = response.headers['set-cookie']?.join('; ') || '';
|
||||
const cookieArray = response.headers['set-cookie'] as string | string[] | undefined;
|
||||
let cookie = '';
|
||||
if (Array.isArray(cookieArray)) {
|
||||
const lastAuth = cookieArray.reverse().find(c => c.startsWith('.ASPXAUTH='));
|
||||
if (lastAuth) {
|
||||
cookie = lastAuth.split(';')[0];
|
||||
}
|
||||
} else if (typeof cookieArray === 'string' && cookieArray.startsWith('.ASPXAUTH=')) {
|
||||
cookie = cookieArray.split(';')[0];
|
||||
}
|
||||
|
||||
console.log('Set-Cookie:', cookieArray);
|
||||
console.log('cookie:', cookie);
|
||||
|
||||
const userId = response.data.Data.UserID;
|
||||
const fullName = response.data.Data.FullName;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Ionicons, MaterialCommunityIcons, MaterialIcons } from '@expo/vector-icons';
|
||||
import axios from 'axios';
|
||||
import { addDays, addMonths, endOfMonth, endOfWeek, format, getMonth, getYear, isSameDay, startOfMonth, startOfWeek, subMonths } from 'date-fns';
|
||||
import { useRouter } from 'expo-router';
|
||||
import * as SecureStore from 'expo-secure-store';
|
||||
@@ -26,52 +25,45 @@ export default function Schedule() {
|
||||
if (!userCookieRaw) {
|
||||
throw new Error('Nincs elmentett cookie, kérlek jelentkezz be újra!');
|
||||
}
|
||||
console.log('Lekért cookie:', userCookieRaw);
|
||||
|
||||
const userCookie = userCookieRaw.split(';')[0];
|
||||
const year = getYear(currentMonth);
|
||||
const month = getMonth(currentMonth) + 1;
|
||||
console.log('Lekérdezett hónap:', year, month);
|
||||
console.log('Beosztás API hívás:', { year, month });
|
||||
console.log('Küldött cookie:', userCookie);
|
||||
|
||||
const response = await axios.post(
|
||||
'https://mymenu.mcdonalds.hu/api/UserDataApi/GetWorkDayMonthList',
|
||||
{ Data: { Year: year, Month: month } },
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'cookie': userCookie,
|
||||
'Origin': 'https://mymenu.mcdonalds.hu',
|
||||
'Referer': 'https://mymenu.mcdonalds.hu/',
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'hu-HU,hu;q=0.9',
|
||||
console.log(userCookie)
|
||||
|
||||
const response = await fetch(`https://mymenu.mcdonalds.hu/api/UserDataApi/GetWorkDayMonthList`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
cookie: userCookie,
|
||||
origin: 'https://mymenu.mcdonalds.hu',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
Data: {
|
||||
Year: year,
|
||||
Month: month
|
||||
}
|
||||
}
|
||||
);
|
||||
})
|
||||
});
|
||||
|
||||
const cookie = response.headers['set-cookie']?.join('; ');
|
||||
if (cookie) {
|
||||
await SecureStore.setItemAsync('cookie', cookie);
|
||||
} else {
|
||||
console.warn('Nincs set-cookie fejléc a válaszban!');
|
||||
}
|
||||
const data = await response.json();
|
||||
|
||||
console.log('API teljes válasz:', response.data);
|
||||
if (!response.data || !response.data.data || !response.data.data.Data) {
|
||||
console.log('API teljes válasz:', data);
|
||||
if (!data || !data.data || !data.data.Data) {
|
||||
console.log('Nincs beosztás adat a válaszban!');
|
||||
setWorkdays([]);
|
||||
return;
|
||||
}
|
||||
|
||||
setWorkdays(response.data.data.Data);
|
||||
} catch (e) {
|
||||
const err = e as any;
|
||||
if (err.response) {
|
||||
console.log('API válasz hiba:', err.response.data);
|
||||
console.log('API error response:', {
|
||||
data: err.response.data,
|
||||
});
|
||||
} else {
|
||||
console.log('Beosztás API hiba:', e);
|
||||
console.log('Network error:', e);
|
||||
}
|
||||
setWorkdays([]);
|
||||
} finally {
|
||||
@@ -151,7 +143,6 @@ export default function Schedule() {
|
||||
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', width: '100%' }}>
|
||||
<View style={[styles.card, { alignSelf: 'center', marginTop: 48, marginBottom: 48 }]}>
|
||||
<Text style={styles.label}>Beosztás naptár</Text>
|
||||
{/* Hibaüzenet, ha nincs bejelentkezve vagy nincs beosztás */}
|
||||
{(!loading && workdays.length === 0) && (
|
||||
<Text style={{ color: '#ff5252', textAlign: 'center', marginBottom: 12, fontWeight: 'bold' }}>
|
||||
Nincs beosztás vagy nem vagy bejelentkezve!
|
||||
|
||||
Reference in New Issue
Block a user