import { View, Text, TextInput, TouchableOpacity, KeyboardAvoidingView, Platform, ActivityIndicator, StyleSheet, ScrollView, } from 'react-native' import { useState } from 'react' import { useRouter } from 'expo-router' import { SafeAreaView } from 'react-native-safe-area-context' import { Ionicons } from '@expo/vector-icons' import { authClient } from '@/lib/auth-client' import { useAuthStore } from '@/store/auth.store' import { getApiBaseUrl } from '@/lib/api-url' import { getOrgSlug } from '@/lib/org-config' export default function RegistrierungScreen() { const router = useRouter() const setSession = useAuthStore((s) => s.setSession) const [name, setName] = useState('') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [passwordConfirm, setPasswordConfirm] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const canSubmit = name.trim().length > 0 && email.trim().length > 0 && password.length >= 8 && password === passwordConfirm && !loading async function handleRegister() { if (!canSubmit) return setLoading(true) setError('') const orgSlug = getOrgSlug() // Step 1: Create better-auth account const signUpResult = await authClient.signUp.email({ name: name.trim(), email: email.trim().toLowerCase(), password, }) if (signUpResult.error) { setError(signUpResult.error.message ?? 'Registrierung fehlgeschlagen.') setLoading(false) return } // Step 2: Create Member record linked to org try { const apiUrl = getApiBaseUrl() const res = await fetch(`${apiUrl}/api/registrierung/${orgSlug}/signup`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: name.trim(), email: email.trim().toLowerCase(), }), }) if (!res.ok) { console.warn('Member-Datensatz konnte nicht angelegt werden:', await res.text()) } } catch (e) { console.warn('Member-API nicht erreichbar:', e) } // Step 3: Sign in to get session const signInResult = await authClient.signIn.email({ email: email.trim().toLowerCase(), password, }) if (signInResult.error) { setError('Konto erstellt, aber Anmeldung fehlgeschlagen. Bitte einloggen.') setLoading(false) router.replace('/(auth)/login' as never) return } const token = (signInResult.data as any)?.session?.token const user = (signInResult.data as any)?.user await setSession(user ? { user } : null, token) setLoading(false) router.replace('/(app)/home' as never) } return ( router.back()} style={styles.backBtn}> I Konto erstellen Registrieren Sie sich für Ihre Innung Name E-Mail-Adresse Passwort Passwort bestätigen 0 && password !== passwordConfirm && styles.inputError]}> {error ? ( {error} ) : null} {loading ? ( ) : ( Registrieren )} router.replace('/(auth)/login' as never)}> Bereits registriert?{' '} Jetzt anmelden ) } const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: '#FFFFFF' }, keyboardView: { flex: 1 }, scrollContent: { flexGrow: 1 }, content: { flex: 1, paddingHorizontal: 24, paddingTop: 16, paddingBottom: 32 }, backBtn: { width: 40, height: 40, borderRadius: 12, backgroundColor: '#F8FAFC', borderWidth: 1, borderColor: '#E2E8F0', alignItems: 'center', justifyContent: 'center', marginBottom: 24, }, logoSection: { alignItems: 'center', marginBottom: 32 }, logoBox: { width: 64, height: 64, backgroundColor: '#003B7E', borderRadius: 18, alignItems: 'center', justifyContent: 'center', marginBottom: 16, }, logoLetter: { color: '#FFFFFF', fontSize: 30, fontWeight: '900' }, title: { fontSize: 26, fontWeight: '800', color: '#0F172A', letterSpacing: -0.5, marginBottom: 4 }, subtitle: { fontSize: 14, color: '#64748B', textAlign: 'center' }, form: { gap: 8, marginBottom: 24 }, inputLabel: { fontSize: 14, fontWeight: '700', color: '#334155' }, inputWrap: { flexDirection: 'row', alignItems: 'center', backgroundColor: '#F8FAFC', borderRadius: 14, borderWidth: 1, borderColor: '#E2E8F0', paddingHorizontal: 12, gap: 8, }, inputError: { borderColor: '#FECACA' }, input: { flex: 1, paddingVertical: 13, color: '#0F172A', fontSize: 15 }, errorBox: { backgroundColor: '#FEF2F2', borderWidth: 1, borderColor: '#FECACA', borderRadius: 12, paddingHorizontal: 14, paddingVertical: 10, }, errorText: { color: '#B91C1C', fontSize: 13 }, submitBtn: { backgroundColor: '#003B7E', borderRadius: 14, paddingVertical: 14, alignItems: 'center', marginTop: 8, }, submitBtnDisabled: { backgroundColor: '#CBD5E1' }, submitContent: { flexDirection: 'row', alignItems: 'center', gap: 6 }, submitLabel: { color: '#FFFFFF', fontWeight: '700', fontSize: 15 }, loginLink: { textAlign: 'center', color: '#64748B', fontSize: 14 }, loginLinkBold: { color: '#003B7E', fontWeight: '700' }, })