import { useRouter } from 'expo-router'; import { useState } from 'react'; import { Alert, Pressable, StyleSheet, Text, TextInput, View } from 'react-native'; import { useAuth } from '../context/AuthContext'; import { ApiError } from '../api/client'; import i18n from '../i18n'; export default function Login() { const router = useRouter(); const { signIn, user } = useAuth(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); const handleSubmit = async () => { if (!username.trim() || !password) { setError(i18n.t('login_error_required')); return; } setSubmitting(true); setError(null); // Changer de compte en étant connecté remplace la session locale (garde-fou). if (user) { const confirmed = await new Promise((resolve) => { Alert.alert(i18n.t('login_switch_title'), i18n.t('login_switch_message'), [ { text: i18n.t('login_switch_cancel'), style: 'cancel', onPress: () => resolve(false) }, { text: i18n.t('login_switch_confirm'), style: 'destructive', onPress: () => resolve(true) }, ]); }); if (!confirmed) { setSubmitting(false); return; } } try { await signIn(username.trim(), password); router.replace('/'); } catch (err) { setError(err instanceof ApiError ? err.message : i18n.t('login_error_generic')); } finally { setSubmitting(false); } }; return ( {i18n.t('login_subtitle')} {error ? {error} : null} {submitting ? i18n.t('login_submitting') : i18n.t('login_submit')} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', padding: 24, justifyContent: 'center', gap: 12, }, title: { fontSize: 18, fontWeight: '600', marginBottom: 8, textAlign: 'center', }, input: { borderWidth: StyleSheet.hairlineWidth, borderColor: '#ccc', borderRadius: 8, paddingHorizontal: 14, paddingVertical: 12, fontSize: 15, }, error: { color: '#c5221f', fontSize: 14, }, button: { backgroundColor: '#1a73e8', paddingVertical: 12, borderRadius: 8, alignItems: 'center', marginTop: 4, }, buttonDisabled: { opacity: 0.6, }, buttonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });