Refactor authentication and user data handling in Index, Profile, and Schedule components to use token-based authentication and improve error handling
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user