import React, { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, StyleSheet, KeyboardAvoidingView, Platform, ActivityIndicator, ScrollView, Image, } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import { router } from 'expo-router'; import { useApp } from '../../context/AppContext'; import { useColors } from '../../constants/Colors'; import { ThemeBackdrop } from '../../components/ThemeBackdrop'; import { AuthService } from '../../services/authService'; export default function LoginScreen() { const { isDarkMode, colorPalette, hydrateSession } = useApp(); const colors = useColors(isDarkMode, colorPalette); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleLogin = async () => { if (!email.trim() || !password) { setError('Bitte alle Felder ausfüllen.'); return; } setLoading(true); setError(null); try { const session = await AuthService.login(email, password); await hydrateSession(session); router.replace('/(tabs)'); } catch (e: any) { if (e.message === 'USER_NOT_FOUND') { setError('Kein Konto mit dieser E-Mail gefunden.'); } else if (e.message === 'WRONG_PASSWORD') { setError('Falsches Passwort.'); } else if (e.message === 'BACKEND_URL_MISSING') { setError('Backend-URL fehlt. Bitte EXPO_PUBLIC_BACKEND_URL konfigurieren.'); } else if (e.message === 'NETWORK_ERROR') { setError('Server nicht erreichbar. Bitte versuche es erneut.'); } else { setError('Anmeldung fehlgeschlagen. Bitte versuche es erneut.'); } } finally { setLoading(false); } }; return ( {/* Logo / Header */} GreenLens Willkommen zurück {/* Card */} {/* Email */} E-Mail {/* Password */} Passwort setShowPassword((v) => !v)} style={styles.eyeBtn}> {/* Error */} {error && ( {error} )} {/* Login Button */} {loading ? ( ) : ( Anmelden )} {/* Divider */} oder {/* Sign Up Link */} router.push('/auth/signup')} activeOpacity={0.82} > Noch kein Konto?{' '} Registrieren ); } const styles = StyleSheet.create({ flex: { flex: 1 }, scroll: { flexGrow: 1, justifyContent: 'center', paddingHorizontal: 24, paddingVertical: 48, }, header: { alignItems: 'center', marginBottom: 32, }, logoIcon: { width: 88, height: 88, borderRadius: 20, marginBottom: 16, }, appName: { fontSize: 30, fontWeight: '700', letterSpacing: -0.5, marginBottom: 6, }, subtitle: { fontSize: 15, fontWeight: '400', }, card: { borderRadius: 20, borderWidth: 1, padding: 24, gap: 16, shadowOffset: { width: 0, height: 4 }, shadowOpacity: 1, shadowRadius: 12, elevation: 4, }, fieldGroup: { gap: 6, }, label: { fontSize: 13, fontWeight: '500', marginLeft: 2, }, inputRow: { flexDirection: 'row', alignItems: 'center', borderWidth: 1, borderRadius: 12, paddingHorizontal: 14, height: 50, }, inputIcon: { marginRight: 10, }, input: { flex: 1, fontSize: 15, height: 50, }, eyeBtn: { padding: 4, marginLeft: 6, }, errorBox: { flexDirection: 'row', alignItems: 'center', gap: 6, borderRadius: 10, paddingHorizontal: 12, paddingVertical: 10, }, errorText: { fontSize: 13, flex: 1, }, primaryBtn: { height: 52, borderRadius: 14, justifyContent: 'center', alignItems: 'center', marginTop: 4, }, primaryBtnText: { fontSize: 16, fontWeight: '600', }, dividerRow: { flexDirection: 'row', alignItems: 'center', marginVertical: 20, gap: 12, }, dividerLine: { flex: 1, height: 1, }, dividerText: { fontSize: 13, }, secondaryBtn: { height: 52, borderRadius: 14, borderWidth: 1, justifyContent: 'center', alignItems: 'center', }, secondaryBtnText: { fontSize: 15, }, });