From dd6627ae35976cf637e7d6ec2d1d8e5b359685bc Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Mon, 28 Jul 2025 13:08:18 +0200 Subject: [PATCH] Refactor cookie handling in Index and update API call in Schedule for improved error logging and response handling --- app/index.tsx | 14 ++++++++++++- app/schedule.tsx | 53 ++++++++++++++++++++---------------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/index.tsx b/app/index.tsx index ea68284..53a8acf 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -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; diff --git a/app/schedule.tsx b/app/schedule.tsx index a2e149e..2dce947 100644 --- a/app/schedule.tsx +++ b/app/schedule.tsx @@ -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() { Beosztás naptár - {/* Hibaüzenet, ha nincs bejelentkezve vagy nincs beosztás */} {(!loading && workdays.length === 0) && ( Nincs beosztás vagy nem vagy bejelentkezve!