import { useState } from "react"; import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert, KeyboardAvoidingView, Platform, ScrollView, } from "react-native"; import { Redirect } from "expo-router"; import * as Linking from "expo-linking"; import { supabase } from "../../src/lib/supabase"; import { useAuthStore } from "../../src/store"; type AuthMode = "magic-link" | "password"; export default function LoginScreen() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [isMagicLinkSent, setIsMagicLinkSent] = useState(false); const [authMode, setAuthMode] = useState("magic-link"); const [isSignUp, setIsSignUp] = useState(false); const { isAuthenticated } = useAuthStore(); // Use declarative Redirect instead of imperative router.replace() // This ensures navigation only happens after the Root Layout is mounted if (isAuthenticated) { return ; } const handleMagicLink = async () => { if (!email.trim()) { Alert.alert("Error", "Please enter your email"); return; } setIsLoading(true); const { error } = await supabase.auth.signInWithOtp({ email: email.trim(), options: { emailRedirectTo: Linking.createURL("/(auth)/login"), }, }); setIsLoading(false); if (error) { Alert.alert("Error", error.message); } else { setIsMagicLinkSent(true); } }; const handlePasswordAuth = async () => { if (!email.trim()) { Alert.alert("Error", "Please enter your email"); return; } if (!password) { Alert.alert("Error", "Please enter your password"); return; } if (isSignUp && password.length < 6) { Alert.alert("Error", "Password must be at least 6 characters"); return; } setIsLoading(true); if (isSignUp) { const { error } = await supabase.auth.signUp({ email: email.trim(), password, }); setIsLoading(false); if (error) { Alert.alert("Error", error.message); } else { Alert.alert( "Check your email", "We sent you a confirmation link to verify your email address.", [{ text: "OK" }] ); } } else { const { error } = await supabase.auth.signInWithPassword({ email: email.trim(), password, }); setIsLoading(false); if (error) { Alert.alert("Error", error.message); } } }; const handleGoogleSignIn = async () => { const { error } = await supabase.auth.signInWithOAuth({ provider: "google", options: { redirectTo: Linking.createURL("/(auth)/login"), }, }); if (error) { Alert.alert("Error", error.message); } }; const handleAppleSignIn = async () => { const { error } = await supabase.auth.signInWithOAuth({ provider: "apple", options: { redirectTo: Linking.createURL("/(auth)/login"), }, }); if (error) { Alert.alert("Error", error.message); } }; if (isMagicLinkSent) { return ( ✉️ Check your email We sent a magic link to {email} setIsMagicLinkSent(false)} > Try a different email ); } return ( 💕 Sauci Explore intimacy together {/* Auth Mode Toggle */} setAuthMode("magic-link")} > Magic Link setAuthMode("password")} > Password {authMode === "password" && ( )} {authMode === "magic-link" ? ( {isLoading ? "Sending..." : "Continue with Magic Link"} ) : ( <> {isLoading ? isSignUp ? "Creating Account..." : "Signing In..." : isSignUp ? "Create Account" : "Sign In"} setIsSignUp(!isSignUp)} > {isSignUp ? "Already have an account? Sign In" : "Don't have an account? Sign Up"} )} or Continue with Google Continue with Apple ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: "#1a1a2e", }, content: { flex: 1, paddingHorizontal: 24, paddingTop: 80, alignItems: "center", }, emoji: { fontSize: 64, marginBottom: 16, }, title: { fontSize: 32, fontWeight: "bold", color: "#fff", marginBottom: 8, }, subtitle: { fontSize: 16, color: "#888", marginBottom: 48, textAlign: "center", }, form: { width: "100%", }, input: { backgroundColor: "#16213e", borderRadius: 12, padding: 16, fontSize: 16, color: "#fff", marginBottom: 16, borderWidth: 1, borderColor: "#0f3460", }, button: { backgroundColor: "#e94560", borderRadius: 12, padding: 16, alignItems: "center", }, buttonDisabled: { opacity: 0.7, }, buttonText: { color: "#fff", fontSize: 16, fontWeight: "600", }, divider: { flexDirection: "row", alignItems: "center", marginVertical: 24, }, dividerLine: { flex: 1, height: 1, backgroundColor: "#333", }, dividerText: { color: "#666", paddingHorizontal: 16, }, socialButton: { backgroundColor: "#16213e", borderRadius: 12, padding: 16, alignItems: "center", marginBottom: 12, borderWidth: 1, borderColor: "#0f3460", }, socialButtonText: { color: "#fff", fontSize: 16, fontWeight: "500", }, secondaryButton: { marginTop: 24, }, secondaryButtonText: { color: "#e94560", fontSize: 16, }, scrollContent: { flexGrow: 1, }, modeToggleContainer: { flexDirection: "row", backgroundColor: "#16213e", borderRadius: 12, padding: 4, marginBottom: 24, width: "100%", }, modeToggle: { flex: 1, paddingVertical: 12, borderRadius: 8, alignItems: "center", }, modeToggleActive: { backgroundColor: "#e94560", }, modeToggleText: { color: "#666", fontSize: 14, fontWeight: "500", }, modeToggleTextActive: { color: "#fff", }, toggleSignUp: { marginTop: 16, alignItems: "center", }, toggleSignUpText: { color: "#e94560", fontSize: 14, }, });