From ccc8b00348bddf9c10d952c0ffc378aa4f4b5276 Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Mon, 28 Jul 2025 13:31:06 +0200 Subject: [PATCH] Refactor authentication and user data handling in Index, Profile, and Schedule components to use token-based authentication and improve error handling --- app/index.tsx | 66 +++++++++++++++++++----------------------------- app/profile.tsx | 55 +++++++++++++++++++++++++++------------- app/schedule.tsx | 27 +++++++------------- 3 files changed, 73 insertions(+), 75 deletions(-) diff --git a/app/index.tsx b/app/index.tsx index 53a8acf..fa4f4fd 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -1,5 +1,4 @@ import { MaterialIcons } from '@expo/vector-icons'; -import axios from 'axios'; import { useRouter } from 'expo-router'; import * as SecureStore from 'expo-secure-store'; import { StatusBar } from 'expo-status-bar'; @@ -20,7 +19,6 @@ export default function Index() { (async () => { const savedEmail = await SecureStore.getItemAsync('email'); const savedPassword = await SecureStore.getItemAsync('password'); - // ... if (savedEmail && savedPassword) { setUsername(savedEmail); setPassword(savedPassword); @@ -36,57 +34,45 @@ export default function Index() { setLoading(true); const user = emailOverride ?? username; const pass = passwordOverride ?? password; - // ... try { - const response = await axios.post( - "https://mymenu.mcdonalds.hu/api/AccountApi/Login", - { - Data: { - UserName: user, - Password: pass - } - }, - { - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - 'referer': 'https://mymenu.mcdonalds.hu/', - 'origin': 'https://mymenu.mcdonalds.hu' - } - } - ); - 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]; + const response = await fetch('https://menuapi.devbeni.lol/api/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }, + body: JSON.stringify({ username: user, password: pass }) + }); + const loginData = await response.json(); + if (!loginData || !loginData.token) { + throw new Error('Hibás bejelentkezés vagy hiányzó token!'); } - console.log('Set-Cookie:', cookieArray); - console.log('cookie:', cookie); - - const userId = response.data.Data.UserID; - const fullName = response.data.Data.FullName; - await SecureStore.setItemAsync('cookie', cookie || ''); - await SecureStore.setItemAsync('userId', String(userId)); + await SecureStore.setItemAsync('token', loginData.token); await SecureStore.setItemAsync('email', user); await SecureStore.setItemAsync('password', pass); - await SecureStore.setItemAsync('fullName', fullName || ''); + + const meResponse = await fetch('https://menuapi.devbeni.lol/api/@me', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${loginData.token}`, + 'Accept': 'application/json', + }, + }); + const meData = await meResponse.json(); + if (meData && meData.data) { + await SecureStore.setItemAsync('fullName', meData.data.fullName || ''); + await SecureStore.setItemAsync('userId', String(meData.data.userId || '')); + } + if (isAuto) { if (Platform.OS === 'android') { ToastAndroid.show('Sikeres automatikus bejelentkezés', ToastAndroid.SHORT); - } else { - // ... } } router.replace('/profile'); } catch (e) { - // ... Alert.alert('Hiba', 'Hibás felhasználónév vagy jelszó, vagy hálózati hiba.'); } finally { setLoading(false); diff --git a/app/profile.tsx b/app/profile.tsx index 8e5e1ae..41646fd 100644 --- a/app/profile.tsx +++ b/app/profile.tsx @@ -9,28 +9,47 @@ const PRIMARY = '#A24BFA'; const BG = '#0c0a0a'; export default function Profile() { - const [fullName, setFullName] = useState(''); - const [email, setEmail] = useState(''); - const [userId, setUserId] = useState(''); + const [user, setUser] = useState(null); + const [loading, setLoading] = useState(false); const router = useRouter(); useEffect(() => { - (async () => { - const name = await SecureStore.getItemAsync('fullName'); - const mail = await SecureStore.getItemAsync('email'); - const uid = await SecureStore.getItemAsync('userId'); - setFullName(name || ''); - setEmail(mail || ''); - setUserId(uid || ''); - })(); + fetchUser(); }, []); + async function fetchUser() { + setLoading(true); + try { + const token = await SecureStore.getItemAsync('token'); + if (!token) { + throw new Error('Nincs elmentett token, kérlek jelentkezz be újra!'); + } + const response = await fetch('https://menuapi.devbeni.lol/api/@me', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + 'Accept': 'application/json', + }, + }); + const data = await response.json(); + if (!data || !data.data || !data.data.Data) { + setUser(null); + return; + } + setUser(data.data.Data); + } catch (e) { + setUser(null); + } finally { + setLoading(false); + } + } + async function handleLogout() { - await SecureStore.deleteItemAsync('cookie'); - await SecureStore.deleteItemAsync('userId'); + await SecureStore.deleteItemAsync('token'); await SecureStore.deleteItemAsync('email'); await SecureStore.deleteItemAsync('password'); await SecureStore.deleteItemAsync('fullName'); + await SecureStore.deleteItemAsync('userId'); router.replace('/'); } @@ -39,11 +58,13 @@ export default function Profile() { Profil Név: - {fullName} - Email: - {email} + {user?.FullName || '-'} UserID: - {userId} + {user?.UserID || '-'} + Szerepkör: + {user?.RoleCode || '-'} + Étterem: + {user?.RestaurantName || '-'} Kijelentkezés diff --git a/app/schedule.tsx b/app/schedule.tsx index 2dce947..0ad4491 100644 --- a/app/schedule.tsx +++ b/app/schedule.tsx @@ -21,40 +21,31 @@ export default function Schedule() { async function fetchWorkdays() { setLoading(true); try { - const userCookieRaw = await SecureStore.getItemAsync('cookie'); - if (!userCookieRaw) { - throw new Error('Nincs elmentett cookie, kérlek jelentkezz be újra!'); + const token = await SecureStore.getItemAsync('token'); + if (!token) { + throw new Error('Nincs elmentett token, kérlek jelentkezz be újra!'); } - const userCookie = userCookieRaw.split(';')[0]; const year = getYear(currentMonth); const month = getMonth(currentMonth) + 1; - console.log(userCookie) - - const response = await fetch(`https://mymenu.mcdonalds.hu/api/UserDataApi/GetWorkDayMonthList`, { - method: 'POST', + const response = await fetch(`https://menuapi.devbeni.lol/api/@me/schedule?year=${year}&month=${month}`, { + method: 'GET', headers: { - 'Content-Type': 'application/json', - cookie: userCookie, - origin: 'https://mymenu.mcdonalds.hu', + 'Authorization': `Bearer ${token}`, + 'Accept': 'application/json', }, - body: JSON.stringify({ - Data: { - Year: year, - Month: month - } - }) }); const data = await response.json(); console.log('API teljes válasz:', data); - if (!data || !data.data || !data.data.Data) { + if (!data || !data.data || !Array.isArray(data.data.Data)) { console.log('Nincs beosztás adat a válaszban!'); setWorkdays([]); return; } + setWorkdays(data.data.Data); } catch (e) { const err = e as any;