import React, { useState } from 'react'; import { View, Text, TextInput, TouchableOpacity, StyleSheet, ScrollView, KeyboardAvoidingView, Platform, ActivityIndicator, Alert, } from 'react-native'; import { useRouter } from 'expo-router'; import { useAuthStore } from '../lib/auth-store'; export default function LoginScreen() { const router = useRouter(); const { login, isLoading, error, clearError } = useAuthStore(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [validationErrors, setValidationErrors] = useState<{ email?: string; password?: string; }>({}); // Validate email format const validateEmail = (email: string): boolean => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); }; // Validate form const validateForm = (): boolean => { const errors: { email?: string; password?: string } = {}; if (!email.trim()) { errors.email = 'Email is required'; } else if (!validateEmail(email)) { errors.email = 'Please enter a valid email'; } if (!password) { errors.password = 'Password is required'; } else if (password.length < 6) { errors.password = 'Password must be at least 6 characters'; } setValidationErrors(errors); return Object.keys(errors).length === 0; }; // Handle login const handleLogin = async () => { clearError(); if (!validateForm()) { return; } try { await login({ email: email.trim().toLowerCase(), password }); router.replace('/(tabs)'); } catch { // Error is handled by the store } }; // Navigate to signup const handleSignup = () => { clearError(); router.push('/(auth)/signup'); }; // Navigate to forgot password const handleForgotPassword = () => { clearError(); router.push('/(auth)/forgot-password'); }; return ( {/* Header */} Welcome Back Sign in to your account {/* Form */} {/* Email Input */} Email { setEmail(text); if (validationErrors.email) { setValidationErrors((prev) => ({ ...prev, email: undefined })); } }} keyboardType="email-address" autoCapitalize="none" autoCorrect={false} autoComplete="email" editable={!isLoading} /> {validationErrors.email && ( {validationErrors.email} )} {/* Password Input */} Password { setPassword(text); if (validationErrors.password) { setValidationErrors((prev) => ({ ...prev, password: undefined })); } }} secureTextEntry={!showPassword} autoCapitalize="none" autoCorrect={false} autoComplete="password" editable={!isLoading} /> setShowPassword(!showPassword)} disabled={isLoading} > {showPassword ? 'Hide' : 'Show'} {validationErrors.password && ( {validationErrors.password} )} {/* Forgot Password Link */} Forgot Password? {/* API Error */} {error && ( {error} )} {/* Sign In Button */} {isLoading ? ( ) : ( Sign In )} {/* Sign Up Link */} Don't have an account? Sign Up {/* App Info */} MoreThanADiagnosis Supporting the kidney disease community ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#FFFFFF', }, scrollContent: { flexGrow: 1, paddingHorizontal: 24, paddingTop: 60, paddingBottom: 40, }, header: { marginBottom: 40, }, title: { fontSize: 32, fontWeight: 'bold', color: '#333333', marginBottom: 8, }, subtitle: { fontSize: 16, color: '#666666', }, form: { flex: 1, }, inputContainer: { marginBottom: 20, }, label: { fontSize: 14, fontWeight: '600', color: '#333333', marginBottom: 8, }, input: { height: 50, borderWidth: 1, borderColor: '#E0E0E0', borderRadius: 8, paddingHorizontal: 16, fontSize: 16, color: '#333333', backgroundColor: '#FAFAFA', }, inputError: { borderColor: '#FF3B30', }, passwordContainer: { position: 'relative', }, passwordInput: { paddingRight: 60, }, showPasswordButton: { position: 'absolute', right: 16, top: 0, bottom: 0, justifyContent: 'center', }, showPasswordText: { fontSize: 14, color: '#007AFF', fontWeight: '600', }, errorText: { fontSize: 12, color: '#FF3B30', marginTop: 4, }, forgotPassword: { alignSelf: 'flex-end', marginBottom: 24, }, forgotPasswordText: { fontSize: 14, color: '#007AFF', fontWeight: '600', }, apiError: { backgroundColor: '#FFF0F0', borderRadius: 8, padding: 12, marginBottom: 16, borderWidth: 1, borderColor: '#FF3B30', }, apiErrorText: { fontSize: 14, color: '#FF3B30', textAlign: 'center', }, button: { height: 50, backgroundColor: '#007AFF', borderRadius: 8, justifyContent: 'center', alignItems: 'center', marginBottom: 24, }, buttonDisabled: { backgroundColor: '#B0B0B0', }, buttonText: { fontSize: 16, fontWeight: '600', color: '#FFFFFF', }, signupContainer: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', }, signupText: { fontSize: 14, color: '#666666', }, signupLink: { fontSize: 14, color: '#007AFF', fontWeight: '600', }, footer: { marginTop: 40, alignItems: 'center', }, footerText: { fontSize: 16, fontWeight: '600', color: '#333333', marginBottom: 4, }, footerSubtext: { fontSize: 12, color: '#999999', }, });