bagsy/apps/mobile/app/(auth)/sign-in.tsx

229 lines
5.8 KiB
TypeScript

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 (
<View style={styles.container}>
<Text style={styles.title}>Bagsy</Text>
<Text style={styles.subtitle}>Family Shopping List</Text>
{error && <Text style={styles.error}>{error}</Text>}
{usePasskey ? (
<>
<Pressable
style={[styles.button, styles.primaryButton]}
onPress={handlePasskeySignIn}
disabled={isLoading}
>
{isLoading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.buttonText}>Sign In with Passkey</Text>
)}
</Pressable>
<Pressable
style={styles.textButton}
onPress={() => setUsePasskey(false)}
disabled={isLoading}
>
<Text style={styles.textButtonText}>Use Email Instead</Text>
</Pressable>
</>
) : (
<>
<TextInput
style={styles.input}
placeholder="Email"
value={email}
onChangeText={setEmail}
editable={!isLoading}
keyboardType="email-address"
autoCapitalize="none"
/>
<TextInput
style={styles.input}
placeholder="Password"
value={password}
onChangeText={setPassword}
secureTextEntry
editable={!isLoading}
/>
<Pressable
style={[styles.button, styles.primaryButton]}
onPress={handleEmailSignIn}
disabled={isLoading}
>
{isLoading ? (
<ActivityIndicator color="#fff" />
) : (
<Text style={styles.buttonText}>Sign In</Text>
)}
</Pressable>
<Pressable
style={styles.textButton}
onPress={() => setUsePasskey(true)}
disabled={isLoading}
>
<Text style={styles.textButtonText}>Use Passkey Instead</Text>
</Pressable>
</>
)}
<View style={styles.divider} />
<Pressable
style={styles.secondaryButton}
onPress={() => router.push('/(auth)/sign-up')}
disabled={isLoading}
>
<Text style={styles.secondaryButtonText}>Create New Account</Text>
</Pressable>
</View>
);
}
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',
},
});