import { signUp, authClient } from "@/src/lib/auth-client"; import { useRouter } from "expo-router"; import { useState } from "react"; import * as AppleAuthentication from "expo-apple-authentication"; import { ActivityIndicator, KeyboardAvoidingView, Platform, Pressable, Text, TextInput, View, } from "react-native"; import { useTranslation } from "react-i18next"; export default function RegisterScreen() { const router = useRouter(); const { t } = useTranslation(); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); async function handleEmailRegister() { if (!name || !email || !password) { setError("Bitte alle Felder ausfüllen"); return; } if (password.length < 8) { setError("Passwort muss mindestens 8 Zeichen lang sein"); return; } setIsLoading(true); setError(null); try { const result = await signUp.email({ name, email, password, callbackURL: "haushaltsApp://onboarding", }); if (result.error) { setError(result.error.message ?? "Registrierung fehlgeschlagen"); return; } // Email verification required — don't set user/session yet router.replace({ pathname: "/(auth)/verify-email", params: { email } }); } catch { setError("Registrierung fehlgeschlagen"); } finally { setIsLoading(false); } } async function handleAppleRegister() { try { const credential = await AppleAuthentication.signInAsync({ requestedScopes: [ AppleAuthentication.AppleAuthenticationScope.FULL_NAME, AppleAuthentication.AppleAuthenticationScope.EMAIL, ], }); if (!credential.identityToken) return; const result = await authClient.signIn.social({ provider: "apple", idToken: { token: credential.identityToken }, }); if (result.error) { setError(result.error.message ?? t('common.error')); return; } // session is handled by authClient interceptor router.replace("/(app)/haushalt"); } catch (err: unknown) { if ((err as { code?: string }).code !== "ERR_CANCELED") { setError(t('login.appleSignInError')); } } } return ( Konto erstellen Starte deinen Haushalts-Manager {error && ( {error} )} {Platform.OS === "ios" && ( <> {t('common.or')} )} Name E-Mail Passwort {isLoading ? ( ) : ( Konto erstellen )} Bereits ein Konto? router.push("/(auth)/login")}> Anmelden ); }