import React, { useState } from 'react'; import { Alert, StyleSheet, View, TextInput, TouchableOpacity, Text, Image, Platform } from 'react-native'; import { FontAwesome5 } from '@expo/vector-icons'; import { supabase } from '../utils/supabase'; import { useTheme, useThemeColors, lightColors, darkColors } from '../utils/themeContext'; export default function Auth() { const { isDarkMode } = useTheme(); const themeColors = useThemeColors(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [isSignUp, setIsSignUp] = useState(false); const [isResetPassword, setIsResetPassword] = useState(false); async function signInWithEmail() { setLoading(true); const { error } = await supabase.auth.signInWithPassword({ email, password, }); if (error) { Alert.alert('Fehler bei der Anmeldung', error.message); } setLoading(false); } async function signUpWithEmail() { setLoading(true); const { error } = await supabase.auth.signUp({ email, password, }); if (error) { Alert.alert('Fehler bei der Registrierung', error.message); } else { Alert.alert( 'Registrierung erfolgreich', 'Bitte überprüfen Sie Ihre E-Mail für den Bestätigungslink.' ); } setLoading(false); } async function resetPassword() { console.log('Reset password called with email:', email); if (!email) { Alert.alert('Fehler', 'Bitte geben Sie Ihre E-Mail-Adresse ein'); return; } setLoading(true); try { const apiUrl = process.env.EXPO_PUBLIC_API_URL || 'https://mana-core-middleware-111768794939.europe-west3.run.app'; const endpoint = `${apiUrl}/auth/reset-password`; console.log('Calling API endpoint:', endpoint); console.log('Request body:', { email }); // Call your backend endpoint for password reset const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email }), }); console.log('Response status:', response.status); const data = await response.json(); console.log('Response data:', data); if (!response.ok) { throw new Error(data.error || 'Fehler beim Zurücksetzen des Passworts'); } Alert.alert( 'E-Mail gesendet', 'Bitte überprüfen Sie Ihre E-Mail für den Link zum Zurücksetzen des Passworts.' ); setIsResetPassword(false); } catch (error: any) { console.error('Reset password error:', error); Alert.alert('Fehler', error.message || 'Netzwerkfehler. Bitte versuchen Sie es später erneut.'); } setLoading(false); } return ( ManaCore {isResetPassword ? 'Passwort zurücksetzen' : isSignUp ? 'Registrieren' : 'Anmelden'} {isResetPassword ? 'Geben Sie Ihre E-Mail-Adresse ein, um Ihr Passwort zurückzusetzen' : isSignUp ? 'Erstellen Sie ein neues Konto, um Mana zu teilen und zu verwalten' : 'Melden Sie sich an, um Ihre Mana zu verwalten'} {!isResetPassword && ( )} { if (isResetPassword) { resetPassword(); } else if (isSignUp) { signUpWithEmail(); } else { signInWithEmail(); } }} disabled={loading} > {loading ? ( Lädt . . . ) : ( {isResetPassword ? 'Link senden' : isSignUp ? 'Registrieren' : 'Anmelden'} )} {Platform.OS === 'web' && !isSignUp && !isResetPassword && ( setIsResetPassword(true)} > Passwort vergessen? )} { if (isResetPassword) { setIsResetPassword(false); } else { setIsSignUp(!isSignUp); } }} > {isResetPassword ? 'Zurück zur Anmeldung' : isSignUp ? 'Bereits ein Konto? Anmelden' : 'Kein Konto? Registrieren'} © {new Date().getFullYear()} ManaCore. Alle Rechte vorbehalten. ); } const styles = StyleSheet.create({ container: { flex: 1, padding: 20, justifyContent: 'center', }, formContainer: { padding: 24, borderRadius: 12, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 3, borderWidth: 1, maxWidth: 500, width: '100%', alignSelf: 'center', }, logoContainer: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', marginBottom: 24, }, logoIcon: { marginRight: 12, }, logoText: { fontSize: 28, fontWeight: 'bold', }, header: { fontSize: 24, fontWeight: 'bold', marginBottom: 8, textAlign: 'center', }, subHeader: { fontSize: 16, textAlign: 'center', marginBottom: 24, }, inputContainer: { flexDirection: 'row', alignItems: 'center', marginBottom: 16, }, inputIcon: { marginRight: 12, }, input: { flex: 1, height: 50, borderWidth: 1, borderRadius: 8, paddingHorizontal: 16, fontSize: 16, }, button: { height: 54, borderRadius: 8, justifyContent: 'center', alignItems: 'center', marginTop: 24, }, buttonText: { color: 'white', fontSize: 18, fontWeight: 'bold', }, loadingContainer: { flexDirection: 'row', alignItems: 'center', }, dotsContainer: { flexDirection: 'row', marginLeft: 4, }, dots: { color: 'white', fontSize: 18, fontWeight: 'bold', marginLeft: 2, }, switchButton: { marginTop: 24, alignItems: 'center', }, switchText: { fontSize: 16, }, forgotPasswordButton: { marginTop: 16, alignItems: 'center', }, forgotPasswordText: { fontSize: 14, }, footer: { marginTop: 40, alignItems: 'center', }, footerText: { fontSize: 12, }, });