import React, { useState } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, KeyboardAvoidingView, Platform } from 'react-native'; import { showAlert } from '../components/NovaAlert'; import ScreenWrapper from '../components/ScreenWrapper'; import Input from '../components/Input'; import Button from '../components/Button'; import useAuthStore from '../store/useAuthStore'; import { colors, fonts, spacing } from '../utils/theme'; export default function RegisterScreen({ navigation }) { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const register = useAuthStore((s) => s.register); const setSession = useAuthStore((s) => s.setSession); const goToOnboarding = () => { navigation.reset({ index: 0, routes: [{ name: 'Onboarding' }] }); }; const handleRegister = async () => { if (!email.trim() || !password.trim()) { showAlert('Error', 'Please fill in all fields'); return; } if (password !== confirmPassword) { showAlert('Error', 'Passwords do not match'); return; } if (password.length < 6) { showAlert('Error', 'Password must be at least 6 characters'); return; } setLoading(true); try { const data = await register(email.trim(), password); // If registration returned a session (offline mode or auto-confirm), log in directly if (data?.session) { setSession(data.session); goToOnboarding(); } else { showAlert( 'Account Created', 'Check your email to confirm your account, then sign in.', [{ text: 'OK', onPress: () => navigation.navigate('Login') }] ); } } catch (error) { showAlert('Registration Failed', error.message); } finally { setLoading(false); } }; return ( Begin Your Journey Create your Nova40 identity