import { fontWeights } from '@/constants/Fonts'; import { supabase } from '@/constants/SupabaseConfig'; import { useTheme } from '@/hooks/useThemeColor'; import React, { useState } from 'react'; import { Alert, KeyboardAvoidingView, Linking, Modal, Platform, StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native'; import { Card } from './ui/Card'; interface AuthOverlayProps { visible: boolean; onClose: () => void; } export const AuthOverlay: React.FC = ({ visible, onClose }) => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [mode, setMode] = useState<'signin' | 'signup' | 'forgot'>('signin'); const [showSuccess, setShowSuccess] = useState(false); const [successEmail, setSuccessEmail] = useState(''); const theme = useTheme(); const styles = StyleSheet.create({ overlay: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.5)', justifyContent: 'center', alignItems: 'center', padding: 20, }, title: { fontSize: 22, fontFamily: fontWeights[600], color: theme.foreground, textAlign: 'center', marginBottom: 16, }, input: { borderWidth: 1, borderColor: theme.border, borderRadius: 12, padding: 12, height: 48, marginBottom: 0, fontSize: 16, color: theme.foreground, backgroundColor: theme.background, }, button: { backgroundColor: theme.primary, borderRadius: 12, height: 48, alignItems: 'center', justifyContent: 'center', marginTop: 8, marginBottom: 16, }, buttonDisabled: { opacity: 0.6, }, buttonText: { color: theme.background, fontSize: 16, fontFamily: fontWeights[600], }, switchButton: { alignItems: 'center', padding: 6, }, switchText: { color: theme.primary, fontSize: 14, fontFamily: fontWeights[500], }, closeButton: { position: 'absolute', top: 12, right: 12, padding: 6, zIndex: 1, }, closeText: { color: theme.mutedForeground, fontSize: 18, fontFamily: fontWeights[500], }, successContainer: { alignItems: 'center', }, successIcon: { width: 64, height: 64, borderRadius: 32, backgroundColor: '#10B981', alignItems: 'center', justifyContent: 'center', marginBottom: 16, }, successIconText: { fontSize: 28, color: '#ffffff', }, successTitle: { fontSize: 24, fontFamily: fontWeights[600], color: theme.foreground, textAlign: 'center', marginBottom: 8, }, successDescription: { fontSize: 15, color: theme.mutedForeground, textAlign: 'center', marginBottom: 6, lineHeight: 20, }, successEmail: { fontSize: 15, fontFamily: fontWeights[600], color: theme.foreground, textAlign: 'center', marginBottom: 16, }, successNote: { backgroundColor: '#10B98120', borderColor: '#10B98140', borderWidth: 1, borderRadius: 8, padding: 12, marginBottom: 20, }, successNoteText: { fontSize: 13, color: '#059669', textAlign: 'center', lineHeight: 18, }, successButtonContainer: { width: '100%', gap: 8, }, legalText: { fontSize: 11, color: theme.mutedForeground, textAlign: 'center', marginTop: 12, lineHeight: 14, paddingHorizontal: 4, }, legalLink: { color: theme.primary, }, }); const handleAuth = async () => { if (!email.trim()) { Alert.alert('Error', 'Please enter your email'); return; } if (mode !== 'forgot' && !password.trim()) { Alert.alert('Error', 'Please enter your password'); return; } setLoading(true); try { if (mode === 'signup') { if (password !== confirmPassword) { Alert.alert('Error', 'Passwords do not match'); return; } if (password.length < 6) { Alert.alert('Error', 'Password must be at least 6 characters'); return; } const { error } = await supabase.auth.signUp({ email: email.trim(), password: password.trim(), }); if (error) { Alert.alert('Sign Up Error', error.message); } else { setSuccessEmail(email.trim()); setShowSuccess(true); } } else if (mode === 'forgot') { const { error } = await supabase.auth.resetPasswordForEmail(email.trim()); if (error) { Alert.alert('Error', error.message); } else { Alert.alert('Success', 'Check your email for a password reset link'); setMode('signin'); } } else { const { error } = await supabase.auth.signInWithPassword({ email: email.trim(), password: password.trim(), }); if (error) { if (error.message === 'Email not confirmed') { Alert.alert( 'Email Not Confirmed', 'Please check your email and click the confirmation link before signing in.', [ { text: 'Cancel', style: 'cancel' }, { text: 'Resend Email', onPress: async () => { const { error: resendError } = await supabase.auth.resend({ type: 'signup', email: email.trim() }); if (resendError) { Alert.alert('Error', resendError.message); } else { Alert.alert('Success', 'Confirmation email sent! Please check your inbox.'); } } } ] ); } else { Alert.alert('Sign In Error', error.message); } } else { onClose(); } } } catch (error) { Alert.alert('Error', 'An unexpected error occurred'); } finally { setLoading(false); } }; const resetForm = () => { setEmail(''); setPassword(''); setConfirmPassword(''); setMode('signin'); setShowSuccess(false); setSuccessEmail(''); }; const handleClose = () => { resetForm(); onClose(); }; const handleBackToSignIn = () => { setShowSuccess(false); setSuccessEmail(''); setMode('signin'); setEmail(''); setPassword(''); setConfirmPassword(''); }; return ( {showSuccess ? ( Check your email We've sent a confirmation link to: {successEmail} Click the link in the email to activate your account. If you don't see the email, check your spam folder. Back to Sign In Close ) : ( <> {mode === 'signup' ? 'Create Account' : mode === 'forgot' ? 'Reset Password' : 'Sign In'} {mode !== 'forgot' && ( )} {mode === 'signup' && ( )} {loading ? 'Loading...' : mode === 'signup' ? 'Sign Up' : mode === 'forgot' ? 'Send Reset Link' : 'Sign In'} {mode === 'signin' && ( setMode('forgot')} > Forgot Password? )} setMode(mode === 'signup' ? 'signin' : 'signup')} > {mode === 'signup' ? 'Already have an account? Sign In' : mode === 'forgot' ? 'Back to Sign In' : 'Need an account? Sign Up'} By continuing, you agree to our{' '} Linking.openURL('https://www.suna.so/legal?tab=terms')} > Terms of Service {' '}and{' '} Linking.openURL('https://www.suna.so/legal?tab=privacy')} > Privacy Policy )} ); };