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);
|
||||
|
||||
@@ -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<any>(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() {
|
||||
<Text style={styles.title}>Profil</Text>
|
||||
<View style={styles.card}>
|
||||
<Text style={styles.label}>Név:</Text>
|
||||
<Text style={styles.value}>{fullName}</Text>
|
||||
<Text style={styles.label}>Email:</Text>
|
||||
<Text style={styles.value}>{email}</Text>
|
||||
<Text style={styles.value}>{user?.FullName || '-'}</Text>
|
||||
<Text style={styles.label}>UserID:</Text>
|
||||
<Text style={styles.value}>{userId}</Text>
|
||||
<Text style={styles.value}>{user?.UserID || '-'}</Text>
|
||||
<Text style={styles.label}>Szerepkör:</Text>
|
||||
<Text style={styles.value}>{user?.RoleCode || '-'}</Text>
|
||||
<Text style={styles.label}>Étterem:</Text>
|
||||
<Text style={styles.value}>{user?.RestaurantName || '-'}</Text>
|
||||
<TouchableOpacity style={styles.logoutButton} onPress={handleLogout}>
|
||||
<Text style={styles.logoutText}>Kijelentkezés</Text>
|
||||
</TouchableOpacity>
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user