203 lines
4.7 KiB
TypeScript
203 lines
4.7 KiB
TypeScript
import { useState } from 'react';
|
|
import {
|
|
View,
|
|
Text,
|
|
TextInput,
|
|
Pressable,
|
|
StyleSheet,
|
|
ActivityIndicator,
|
|
ScrollView,
|
|
} from 'react-native';
|
|
import { useRouter } from 'expo-router';
|
|
import { useAuthStore } from '@/store/auth';
|
|
import { authClient } from '@/lib/auth-client';
|
|
|
|
/**
|
|
* Sign up screen
|
|
* Creates new account with email and password
|
|
*/
|
|
export default function SignUpScreen() {
|
|
const router = useRouter();
|
|
const { setUser } = useAuthStore();
|
|
const [email, setEmail] = useState('');
|
|
const [password, setPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [name, setName] = useState('');
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSignUp = async () => {
|
|
setError('');
|
|
|
|
if (!email || !password || !confirmPassword || !name) {
|
|
setError('All fields are required');
|
|
return;
|
|
}
|
|
|
|
if (password !== confirmPassword) {
|
|
setError('Passwords do not match');
|
|
return;
|
|
}
|
|
|
|
if (password.length < 6) {
|
|
setError('Password must be at least 6 characters');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
const session = await authClient.signUp.email({
|
|
email,
|
|
password,
|
|
name,
|
|
});
|
|
|
|
const sessionData = (session as any)?.data;
|
|
if (sessionData && (sessionData.user || sessionData.data?.user)) {
|
|
const user = sessionData.user || sessionData.data?.user;
|
|
setUser(user);
|
|
// Go to passkey prompt (optional, skippable)
|
|
router.replace('/(auth)/passkey-prompt');
|
|
}
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Sign up failed';
|
|
setError(message);
|
|
console.error('Sign up error:', err);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ScrollView style={styles.container}>
|
|
<View style={styles.content}>
|
|
<Text style={styles.title}>Create Account</Text>
|
|
<Text style={styles.subtitle}>Join Bagsy today</Text>
|
|
|
|
{error && <Text style={styles.error}>{error}</Text>}
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Full Name"
|
|
value={name}
|
|
onChangeText={setName}
|
|
editable={!isLoading}
|
|
autoCapitalize="words"
|
|
/>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Email"
|
|
value={email}
|
|
onChangeText={setEmail}
|
|
editable={!isLoading}
|
|
keyboardType="email-address"
|
|
autoCapitalize="none"
|
|
/>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Password (min 6 characters)"
|
|
value={password}
|
|
onChangeText={setPassword}
|
|
secureTextEntry
|
|
editable={!isLoading}
|
|
/>
|
|
|
|
<TextInput
|
|
style={styles.input}
|
|
placeholder="Confirm Password"
|
|
value={confirmPassword}
|
|
onChangeText={setConfirmPassword}
|
|
secureTextEntry
|
|
editable={!isLoading}
|
|
/>
|
|
|
|
<Pressable
|
|
style={[styles.button, styles.primaryButton]}
|
|
onPress={handleSignUp}
|
|
disabled={isLoading}
|
|
>
|
|
{isLoading ? (
|
|
<ActivityIndicator color="#fff" />
|
|
) : (
|
|
<Text style={styles.buttonText}>Create Account</Text>
|
|
)}
|
|
</Pressable>
|
|
|
|
<View style={styles.divider} />
|
|
|
|
<Pressable style={styles.textButton} onPress={() => router.back()} disabled={isLoading}>
|
|
<Text style={styles.textButtonText}>Already have an account? Sign In</Text>
|
|
</Pressable>
|
|
</View>
|
|
</ScrollView>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
flex: 1,
|
|
backgroundColor: '#fff',
|
|
},
|
|
content: {
|
|
padding: 20,
|
|
paddingVertical: 40,
|
|
},
|
|
title: {
|
|
fontSize: 28,
|
|
fontWeight: 'bold',
|
|
marginBottom: 8,
|
|
},
|
|
subtitle: {
|
|
fontSize: 16,
|
|
color: '#666',
|
|
marginBottom: 24,
|
|
},
|
|
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',
|
|
},
|
|
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',
|
|
padding: 12,
|
|
backgroundColor: '#ffe5e5',
|
|
borderRadius: 6,
|
|
},
|
|
});
|