1- import React , { useState } from 'react' ;
2- import { View , StyleSheet , TouchableOpacity , Text , Image , KeyboardAvoidingView , Platform , ScrollView } from 'react-native' ;
1+ import React , { useState , useEffect } from 'react' ;
2+ import { View , StyleSheet , TouchableOpacity , Text , Image , KeyboardAvoidingView , Platform , ScrollView , Alert } from 'react-native' ;
33import { useThemeColor } from '../hooks/useThemeColor' ;
44import { Button } from '../components/ui/Button' ;
55import { Input } from '../components/ui/Input' ;
66import { Checkbox } from '../components/ui/Checkbox' ;
77import { router } from 'expo-router' ;
8- import { useUser } from '@/contexts/UserContext ' ;
8+ import { useAuth } from '@/contexts/AuthContext ' ;
99
1010export default function LoginScreen ( ) {
11- const { setName : setUserName } = useUser ( ) ;
11+ const { login , isLoading , isAuthenticated } = useAuth ( ) ;
1212 const [ email , setEmail ] = useState ( '' ) ;
1313 const [ password , setPassword ] = useState ( '' ) ;
1414 const [ rememberMe , setRememberMe ] = useState ( false ) ;
1515 const [ showPassword , setShowPassword ] = useState ( false ) ;
16- const [ isLoading , setIsLoading ] = useState ( false ) ;
1716 const [ errors , setErrors ] = useState < { email ?: string ; password ?: string } > ( { } ) ;
1817
1918 const backgroundColor = useThemeColor ( { } , 'background' ) ;
2019 const textColor = useThemeColor ( { } , 'text' ) ;
2120 const tintColor = useThemeColor ( { } , 'tint' ) ;
21+
22+ // Redirect if already authenticated
23+ useEffect ( ( ) => {
24+ if ( isAuthenticated ) {
25+ router . replace ( '/(tabs)' ) ;
26+ }
27+ } , [ isAuthenticated ] ) ;
2228
2329 const handleLogin = async ( ) => {
2430 setErrors ( { } ) ;
@@ -36,20 +42,16 @@ export default function LoginScreen() {
3642 return ;
3743 }
3844
39- setIsLoading ( true ) ;
40-
4145 try {
42- // Simulate API call
43- await new Promise ( resolve => setTimeout ( resolve , 1500 ) ) ;
44-
45- // Set the name (this mimics what signup does)
46- setUserName ( 'Returning User' ) ; // You can replace this with a real name from API later
47-
48- router . replace ( '/(tabs)' ) ;
46+ await login ( email , password ) ;
47+ // Navigation will happen automatically via useEffect when isAuthenticated becomes true
4948 } catch ( error ) {
5049 console . error ( 'Login error:' , error ) ;
51- } finally {
52- setIsLoading ( false ) ;
50+ Alert . alert (
51+ 'Login Failed' ,
52+ error instanceof Error ? error . message : 'An unexpected error occurred. Please try again.' ,
53+ [ { text : 'OK' } ]
54+ ) ;
5355 }
5456 } ;
5557
@@ -100,7 +102,12 @@ export default function LoginScreen() {
100102 secureTextEntry = { ! showPassword }
101103 error = { errors . password }
102104 rightIcon = {
103- < TouchableOpacity onPress = { ( ) => setShowPassword ( ! showPassword ) } >
105+ < TouchableOpacity
106+ onPress = { ( ) => setShowPassword ( ! showPassword ) }
107+ accessibilityLabel = { showPassword ? 'Hide password' : 'Show password' }
108+ accessibilityRole = "button"
109+ accessible = { true }
110+ >
104111 < Text style = { { color : tintColor } } > { showPassword ? 'Hide' : 'Show' } </ Text >
105112 </ TouchableOpacity >
106113 }
@@ -114,7 +121,12 @@ export default function LoginScreen() {
114121 label = "Remember me"
115122 />
116123
117- < TouchableOpacity onPress = { handleForgotPassword } >
124+ < TouchableOpacity
125+ onPress = { handleForgotPassword }
126+ accessibilityLabel = "Forgot Password"
127+ accessibilityRole = "button"
128+ accessible = { true }
129+ >
118130 < Text style = { [ styles . forgotPassword , { color : tintColor } ] } > Forgot Password?</ Text >
119131 </ TouchableOpacity >
120132 </ View >
@@ -130,7 +142,12 @@ export default function LoginScreen() {
130142
131143 < View style = { styles . signupContainer } >
132144 < Text style = { { color : textColor } } > Don't have an account? </ Text >
133- < TouchableOpacity onPress = { handleSignUp } >
145+ < TouchableOpacity
146+ onPress = { handleSignUp }
147+ accessibilityLabel = "Sign Up"
148+ accessibilityRole = "button"
149+ accessible = { true }
150+ >
134151 < Text style = { { color : tintColor , fontWeight : '600' } } > Sign Up</ Text >
135152 </ TouchableOpacity >
136153 </ View >
0 commit comments