import { useState } from 'react'; import { View, Text, TextInput, Pressable, StyleSheet, ActivityIndicator } from 'react-native'; import { useRouter } from 'expo-router'; import { useAuthStore } from '@/store/auth'; import { authClient } from '@/lib/auth-client'; /** * Sign in screen * Supports: Passkey (WebAuthn) primary, email + password fallback */ export default function SignInScreen() { const router = useRouter(); const { setUser, setIsLoading: setAuthLoading, setError: setAuthError } = useAuthStore(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [usePasskey, setUsePasskey] = useState(true); const [error, setError] = useState(''); const handlePasskeySignIn = async () => { try { setIsLoading(true); setError(''); const response = await authClient.signIn.passkey(); const session = (response as any)?.data; if (session && (session.user || session.data?.user)) { const user = session.user || session.data?.user; setUser(user); router.replace('/(app)'); } } catch (err) { const message = err instanceof Error ? err.message : 'Passkey sign-in failed'; setError(message); console.error('Passkey sign-in error:', err); } finally { setIsLoading(false); } }; const handleEmailSignIn = async () => { if (!email || !password) { setError('Email and password are required'); return; } try { setIsLoading(true); setError(''); const response = await authClient.signIn.email({ email, password, }); const session = (response as any)?.data; if (session && (session.user || session.data?.user)) { const user = session.user || session.data?.user; setUser(user); router.replace('/(app)'); } } catch (err) { const message = err instanceof Error ? err.message : 'Sign in failed'; setError(message); console.error('Email sign-in error:', err); } finally { setIsLoading(false); } }; return ( Bagsy Family Shopping List {error && {error}} {usePasskey ? ( <> {isLoading ? ( ) : ( Sign In with Passkey )} setUsePasskey(false)} disabled={isLoading} > Use Email Instead ) : ( <> {isLoading ? ( ) : ( Sign In )} setUsePasskey(true)} disabled={isLoading} > Use Passkey Instead )} router.push('/(auth)/sign-up')} disabled={isLoading} > Create New Account ); } const styles = StyleSheet.create({ container: { flex: 1, padding: 20, justifyContent: 'center', backgroundColor: '#fff', }, title: { fontSize: 32, fontWeight: 'bold', textAlign: 'center', marginBottom: 8, }, subtitle: { fontSize: 16, color: '#666', textAlign: 'center', marginBottom: 32, }, input: { borderWidth: 1, borderColor: '#ddd', borderRadius: 8, padding: 12, marginBottom: 12, fontSize: 16, }, button: { paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8, alignItems: 'center', justifyContent: 'center', marginVertical: 8, }, primaryButton: { backgroundColor: '#007AFF', }, buttonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, secondaryButton: { borderWidth: 1, borderColor: '#007AFF', paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8, alignItems: 'center', justifyContent: 'center', }, secondaryButtonText: { color: '#007AFF', fontSize: 16, fontWeight: '600', }, textButton: { paddingVertical: 12, alignItems: 'center', }, textButtonText: { color: '#007AFF', fontSize: 14, }, divider: { height: 1, backgroundColor: '#eee', marginVertical: 24, }, error: { color: '#ff3333', fontSize: 14, marginBottom: 12, textAlign: 'center', }, });