// src/screens/LoginScreen.tsx import React, { useState } from 'react'; import { View, StyleSheet, KeyboardAvoidingView, Platform } from 'react-native'; import { TextInput, Button, Text, useTheme, HelperText, ActivityIndicator, Avatar } from 'react-native-paper'; import { useAuth } from '../contexts/AuthContext'; import { NativeStackScreenProps } from '@react-navigation/native-stack'; import { AuthStackParamList } from '../types/navigation'; // Import from the new types file type LoginScreenProps = NativeStackScreenProps; const LoginScreen: React.FC = ({ navigation }) => { const theme = useTheme(); const { login } = useAuth(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const handleLogin = async () => { console.log("[LoginScreen] handleLogin: Button pressed."); // Log: Button Press if (!username || !password) { console.log("[LoginScreen] handleLogin: Missing username or password."); setError('Please enter both username/email and password.'); return; } setError(null); setIsLoading(true); try { // --- Add Log Here --- console.log("[LoginScreen] handleLogin: Calling context login function..."); await login(username, password); console.log("[LoginScreen] handleLogin: Context login function call finished (likely successful navigation)."); // If successful, this component might unmount before this log appears fully. } catch (err: any) { console.log("[LoginScreen] handleLogin: Caught error from context login."); // Log: Error caught const errorMessage = err.response?.data?.detail || err.response?.data?.message || err.message || 'Login failed. Please check your credentials.'; setError(errorMessage); // **Important**: Set loading false *only in the catch block* if navigation doesn't happen setIsLoading(false); console.log("[LoginScreen] handleLogin: Set loading to false after error."); } // **Remove potential premature setIsLoading(false) if it was outside the catch block** }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, backgroundColor: theme.colors.background, }, logoContainer: { alignItems: 'center', marginBottom: 40, }, title: { fontSize: 24, fontWeight: 'bold', textAlign: 'center', marginBottom: 20, color: theme.colors.primary, }, input: { marginBottom: 15, }, button: { marginTop: 10, paddingVertical: 8, // Make button taller }, errorText: { // Use HelperText's styling by setting type='error' textAlign: 'center', marginBottom: 10, }, loadingContainer: { marginTop: 20, } }); return ( MAIA Login {/* Display Error Message */} {error} {/* Show loading indicator inline with button or replace it */} {isLoading ? ( ) : ( <> {/* Add Register Button */} )} {/* TODO: Add Register here - REMOVED */} ); }; export default LoginScreen;