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 { MaterialIcons } from '@expo/vector-icons';
|
||||||
import axios from 'axios';
|
|
||||||
import { useRouter } from 'expo-router';
|
import { useRouter } from 'expo-router';
|
||||||
import * as SecureStore from 'expo-secure-store';
|
import * as SecureStore from 'expo-secure-store';
|
||||||
import { StatusBar } from 'expo-status-bar';
|
import { StatusBar } from 'expo-status-bar';
|
||||||
@@ -20,7 +19,6 @@ export default function Index() {
|
|||||||
(async () => {
|
(async () => {
|
||||||
const savedEmail = await SecureStore.getItemAsync('email');
|
const savedEmail = await SecureStore.getItemAsync('email');
|
||||||
const savedPassword = await SecureStore.getItemAsync('password');
|
const savedPassword = await SecureStore.getItemAsync('password');
|
||||||
// ...
|
|
||||||
if (savedEmail && savedPassword) {
|
if (savedEmail && savedPassword) {
|
||||||
setUsername(savedEmail);
|
setUsername(savedEmail);
|
||||||
setPassword(savedPassword);
|
setPassword(savedPassword);
|
||||||
@@ -36,57 +34,45 @@ export default function Index() {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
const user = emailOverride ?? username;
|
const user = emailOverride ?? username;
|
||||||
const pass = passwordOverride ?? password;
|
const pass = passwordOverride ?? password;
|
||||||
// ...
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
|
||||||
"https://mymenu.mcdonalds.hu/api/AccountApi/Login",
|
const response = await fetch('https://menuapi.devbeni.lol/api/login', {
|
||||||
{
|
method: 'POST',
|
||||||
Data: {
|
|
||||||
UserName: user,
|
|
||||||
Password: pass
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'referer': 'https://mymenu.mcdonalds.hu/',
|
},
|
||||||
'origin': 'https://mymenu.mcdonalds.hu'
|
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!');
|
||||||
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);
|
await SecureStore.setItemAsync('token', loginData.token);
|
||||||
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('email', user);
|
await SecureStore.setItemAsync('email', user);
|
||||||
await SecureStore.setItemAsync('password', pass);
|
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 (isAuto) {
|
||||||
if (Platform.OS === 'android') {
|
if (Platform.OS === 'android') {
|
||||||
ToastAndroid.show('Sikeres automatikus bejelentkezés', ToastAndroid.SHORT);
|
ToastAndroid.show('Sikeres automatikus bejelentkezés', ToastAndroid.SHORT);
|
||||||
} else {
|
|
||||||
// ...
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
router.replace('/profile');
|
router.replace('/profile');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// ...
|
|
||||||
Alert.alert('Hiba', 'Hibás felhasználónév vagy jelszó, vagy hálózati hiba.');
|
Alert.alert('Hiba', 'Hibás felhasználónév vagy jelszó, vagy hálózati hiba.');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
@@ -9,28 +9,47 @@ const PRIMARY = '#A24BFA';
|
|||||||
const BG = '#0c0a0a';
|
const BG = '#0c0a0a';
|
||||||
|
|
||||||
export default function Profile() {
|
export default function Profile() {
|
||||||
const [fullName, setFullName] = useState('');
|
const [user, setUser] = useState<any>(null);
|
||||||
const [email, setEmail] = useState('');
|
const [loading, setLoading] = useState(false);
|
||||||
const [userId, setUserId] = useState('');
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
fetchUser();
|
||||||
const name = await SecureStore.getItemAsync('fullName');
|
|
||||||
const mail = await SecureStore.getItemAsync('email');
|
|
||||||
const uid = await SecureStore.getItemAsync('userId');
|
|
||||||
setFullName(name || '');
|
|
||||||
setEmail(mail || '');
|
|
||||||
setUserId(uid || '');
|
|
||||||
})();
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
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() {
|
async function handleLogout() {
|
||||||
await SecureStore.deleteItemAsync('cookie');
|
await SecureStore.deleteItemAsync('token');
|
||||||
await SecureStore.deleteItemAsync('userId');
|
|
||||||
await SecureStore.deleteItemAsync('email');
|
await SecureStore.deleteItemAsync('email');
|
||||||
await SecureStore.deleteItemAsync('password');
|
await SecureStore.deleteItemAsync('password');
|
||||||
await SecureStore.deleteItemAsync('fullName');
|
await SecureStore.deleteItemAsync('fullName');
|
||||||
|
await SecureStore.deleteItemAsync('userId');
|
||||||
router.replace('/');
|
router.replace('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,11 +58,13 @@ export default function Profile() {
|
|||||||
<Text style={styles.title}>Profil</Text>
|
<Text style={styles.title}>Profil</Text>
|
||||||
<View style={styles.card}>
|
<View style={styles.card}>
|
||||||
<Text style={styles.label}>Név:</Text>
|
<Text style={styles.label}>Név:</Text>
|
||||||
<Text style={styles.value}>{fullName}</Text>
|
<Text style={styles.value}>{user?.FullName || '-'}</Text>
|
||||||
<Text style={styles.label}>Email:</Text>
|
|
||||||
<Text style={styles.value}>{email}</Text>
|
|
||||||
<Text style={styles.label}>UserID:</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}>
|
<TouchableOpacity style={styles.logoutButton} onPress={handleLogout}>
|
||||||
<Text style={styles.logoutText}>Kijelentkezés</Text>
|
<Text style={styles.logoutText}>Kijelentkezés</Text>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
|
|||||||
@@ -21,40 +21,31 @@ export default function Schedule() {
|
|||||||
async function fetchWorkdays() {
|
async function fetchWorkdays() {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const userCookieRaw = await SecureStore.getItemAsync('cookie');
|
const token = await SecureStore.getItemAsync('token');
|
||||||
if (!userCookieRaw) {
|
if (!token) {
|
||||||
throw new Error('Nincs elmentett cookie, kérlek jelentkezz be újra!');
|
throw new Error('Nincs elmentett token, kérlek jelentkezz be újra!');
|
||||||
}
|
}
|
||||||
|
|
||||||
const userCookie = userCookieRaw.split(';')[0];
|
|
||||||
const year = getYear(currentMonth);
|
const year = getYear(currentMonth);
|
||||||
const month = getMonth(currentMonth) + 1;
|
const month = getMonth(currentMonth) + 1;
|
||||||
|
|
||||||
console.log(userCookie)
|
const response = await fetch(`https://menuapi.devbeni.lol/api/@me/schedule?year=${year}&month=${month}`, {
|
||||||
|
method: 'GET',
|
||||||
const response = await fetch(`https://mymenu.mcdonalds.hu/api/UserDataApi/GetWorkDayMonthList`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Authorization': `Bearer ${token}`,
|
||||||
cookie: userCookie,
|
'Accept': 'application/json',
|
||||||
origin: 'https://mymenu.mcdonalds.hu',
|
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
|
||||||
Data: {
|
|
||||||
Year: year,
|
|
||||||
Month: month
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
console.log('API teljes válasz:', data);
|
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!');
|
console.log('Nincs beosztás adat a válaszban!');
|
||||||
setWorkdays([]);
|
setWorkdays([]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
setWorkdays(data.data.Data);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const err = e as any;
|
const err = e as any;
|
||||||
|
|||||||
Reference in New Issue
Block a user