import { useAlert } from '@/aesthetics/Alert'; import { Text } from '@/aesthetics/Text'; import { TextInput } from '@/aesthetics/TextInput'; import { font, radius, shadows, spacing, useColors } from '@/aesthetics/styles'; import { useStrings } from '@/common/hooks/useStrings'; import { useAuth } from '@/profile/auth/context'; import { Stack, useRouter } from 'expo-router'; import { ArrowRight, Mail } from 'lucide-react-native'; import { useState } from 'react'; import { ActivityIndicator, KeyboardAvoidingView, Platform, Pressable, StyleSheet, View, } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; export default function SignInScreen() { const insets = useSafeAreaInsets(); const router = useRouter(); const c = useColors(); const strings = useStrings(); const { showAlert } = useAlert(); const { signInWithEmail, verifyOtp } = useAuth(); const [email, setEmail] = useState(''); const [code, setCode] = useState(''); const [step, setStep] = useState<'email' | 'code'>('email'); const [isLoading, setIsLoading] = useState(false); const handleSendCode = async () => { if (!email || !email.includes('@')) { showAlert(strings.common.error, strings.auth.errors.invalidEmail); return; } setIsLoading(true); const result = await signInWithEmail(email); setIsLoading(false); if (result.success) { setStep('code'); } else { showAlert(strings.common.error, result.message || strings.auth.errors.sendFailed); } }; const handleVerifyCode = async () => { if (!code || code.length < 6) { showAlert(strings.common.error, strings.auth.errors.invalidCode); return; } setIsLoading(true); const result = await verifyOtp(email, code); setIsLoading(false); if (result.success) { if (router.canGoBack()) { router.back(); } else { router.replace('/(tabs)'); } } else { showAlert(strings.common.error, result.message || strings.auth.errors.invalidCodeBackend); } }; return ( {step === 'email' ? strings.auth.welcome : strings.common.verify} {step === 'email' ? strings.auth.signInSubtitle : strings.auth.sentCodeTo(email)} {step === 'email' ? ( ) : ( )} [ styles.button, shadows.sm, { backgroundColor: c.accent }, pressed && styles.pressed, ]} onPress={step === 'email' ? handleSendCode : handleVerifyCode} disabled={isLoading} > {isLoading ? ( ) : ( {step === 'email' ? strings.common.continue : strings.auth.verifyCode} )} {step === 'code' && ( [styles.backButton, pressed && styles.pressed]} onPress={() => setStep('email')} disabled={isLoading} > {strings.auth.differentEmail} )} ); } const styles = StyleSheet.create({ container: { flex: 1, }, keyboardView: { flex: 1, }, content: { flex: 1, paddingHorizontal: spacing.xl, }, header: { marginBottom: spacing.xxxl, }, title: { fontSize: font.display, letterSpacing: 0.5, marginBottom: spacing.sm, }, subtitle: { fontSize: font.md, lineHeight: 24, }, form: { gap: spacing.lg, }, inputContainer: { flexDirection: 'row', alignItems: 'center', height: 60, borderRadius: radius.lg, borderWidth: 1, paddingHorizontal: spacing.md, gap: spacing.md, }, inputIcon: { width: 40, height: 40, borderRadius: radius.md, alignItems: 'center', justifyContent: 'center', }, input: { flex: 1, fontSize: font.md, height: '100%', }, codeInput: { justifyContent: 'center', }, codeInputText: { textAlign: 'center', letterSpacing: 8, fontSize: font.xxl, fontWeight: '600', }, button: { height: 56, borderRadius: radius.lg, alignItems: 'center', justifyContent: 'center', }, buttonContent: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, }, buttonText: { fontSize: font.md, fontWeight: '600', }, backButton: { alignItems: 'center', padding: spacing.md, }, backButtonText: { fontSize: font.sm, fontWeight: '500', }, pressed: { opacity: 0.7, }, });