- Refactor Profile and Login screens to use AuthContext for user data
- Enhance RequestPermitModal with multiple time-off types and validation - Implement CalendarWidget for visualizing time-off requests - Improve API error handling and token management - Add utility functions for consistent date and time formatting - Clean up unused mock data and update types
This commit is contained in:
@ -8,24 +8,66 @@ import {
|
||||
TextInput,
|
||||
TouchableOpacity,
|
||||
View,
|
||||
Image
|
||||
Image,
|
||||
Alert
|
||||
} from 'react-native';
|
||||
import { AuthContext } from '@/utils/authContext';
|
||||
import api from '@/utils/api';
|
||||
|
||||
export default function LoginScreen() {
|
||||
const authContext = useContext(AuthContext);
|
||||
const [email, setEmail] = useState('');
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const handleLogin = () => {
|
||||
const handleLogin = async () => {
|
||||
// TODO: Implementa toast o messaggio di errore più user-friendly
|
||||
if (!username || !password) {
|
||||
Alert.alert("Attenzione", "Inserisci username e password");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
// Simulazione login
|
||||
setTimeout(() => {
|
||||
|
||||
try {
|
||||
console.log("Tentativo login con:", username);
|
||||
|
||||
// Chiamata vera al backend
|
||||
const response = await api.post("/user/login", {
|
||||
username: username,
|
||||
password: password
|
||||
});
|
||||
|
||||
const { token, user } = response.data;
|
||||
|
||||
console.log("Login successo, token ricevuto.");
|
||||
console.log("Dati utente:", user);
|
||||
|
||||
// Passiamo token e dati utente al context che gestirà salvataggio e redirect
|
||||
authContext.logIn(token, user);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error("Login Error:", error);
|
||||
|
||||
let message = "Si è verificato un errore durante l'accesso.";
|
||||
|
||||
if (error.response) {
|
||||
// Errore dal server (es. 401 Credenziali errate)
|
||||
if (error.response.status === 401) {
|
||||
message = "Credenziali non valide.";
|
||||
} else {
|
||||
message = `Errore Server: ${error.response.data.message || error.response.status}`;
|
||||
}
|
||||
} else if (error.request) {
|
||||
// Server non raggiungibile
|
||||
message = "Impossibile contattare il server. Controlla la connessione.";
|
||||
}
|
||||
|
||||
Alert.alert("Login Fallito", message);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
authContext.logIn();
|
||||
}, 1500);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@ -47,17 +89,17 @@ export default function LoginScreen() {
|
||||
>
|
||||
<ScrollView showsVerticalScrollIndicator={false} className="h-full">
|
||||
<View className="gap-6 flex flex-col" style={{ gap: '1.5rem' }}>
|
||||
{/* Input Email */}
|
||||
{/* Input Username / Email */}
|
||||
<View>
|
||||
<Text className="text-gray-700 text-lg font-bold mb-3 ml-1">Email o Username</Text>
|
||||
<Text className="text-gray-700 text-lg font-bold mb-3 ml-1">Username o Email</Text>
|
||||
<View className="flex-row items-center bg-gray-50 border border-gray-100 rounded-2xl h-16 px-4 flex">
|
||||
<Mail size={24} color="#9ca3af" />
|
||||
<TextInput
|
||||
className="flex-1 ml-4 text-gray-800 text-lg font-medium h-full w-full"
|
||||
placeholder="mario.rossi@esempio.com"
|
||||
placeholderTextColor="#9ca3af"
|
||||
value={email}
|
||||
onChangeText={setEmail}
|
||||
value={username}
|
||||
onChangeText={setUsername}
|
||||
autoCapitalize="none"
|
||||
keyboardType="email-address"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user