1
1
import { View , StyleSheet , ScrollView } from "react-native" ;
2
- import React from "react" ;
2
+ import { useState } from "react" ;
3
3
import { Button , Input , Text } from "@rneui/themed" ;
4
4
import { COLORS } from "../../constants" ;
5
+ import useAuth from "../../hooks/useAuth" ;
5
6
6
7
const Login = ( { navigation } ) => {
8
+ // Variables
9
+ const { login } = useAuth ( ) ;
10
+ const [ email , setEmail ] = useState ( { value : null , errorMessage : null } ) ;
11
+ const [ password , setPassword ] = useState ( { value : null , errorMessage : null } ) ;
12
+
13
+ // Event handlers
7
14
const handleRegisterNowPress = ( ) => {
8
15
navigation . navigate ( "Register" ) ;
9
16
} ;
17
+
18
+ const handleLoginPress = ( ) => {
19
+ setEmail ( { ...email , errorMessage : null } ) ;
20
+ setPassword ( { ...password , errorMessage : null } ) ;
21
+ if ( ! email . value ) {
22
+ setEmail ( { ...email , errorMessage : "Please fill out all fields." } ) ;
23
+ }
24
+ if ( ! password . value ) {
25
+ setPassword ( { ...password , errorMessage : "Please fill out all fields." } ) ;
26
+ }
27
+ if ( ! ( email . value && password . value ) ) {
28
+ return ;
29
+ }
30
+
31
+ try {
32
+ login ( email . value , password . value ) ;
33
+ } catch ( error ) {
34
+ console . error ( "Error while loggin in: " , error ) ;
35
+ }
36
+ } ;
37
+
10
38
return (
11
39
< ScrollView
12
40
style = { { width : "100%" } }
@@ -18,25 +46,41 @@ const Login = ({ navigation }) => {
18
46
paddingHorizontal : 30 ,
19
47
} }
20
48
>
21
- < Text h2 > Register </ Text >
49
+ < Text h2 > Login </ Text >
22
50
< View
23
51
style = { { width : "100%" , alignItems : "center" , paddingVertical : 40 } }
24
52
>
25
- < Input label = { "Full Name" } labelStyle = { styles . inputLabel } />
26
- < Input label = { "Email" } labelStyle = { styles . inputLabel } />
27
- < Input label = { "Password" } labelStyle = { styles . inputLabel } />
28
- < Input label = { "Confirm Password" } labelStyle = { styles . inputLabel } />
53
+ < Input
54
+ label = { "Email" }
55
+ labelStyle = { styles . inputLabel }
56
+ value = { email . value }
57
+ onChangeText = { ( value ) => setEmail ( { ...email , value : value } ) }
58
+ errorMessage = { email . errorMessage }
59
+ />
60
+ < Input
61
+ label = { "Password" }
62
+ labelStyle = { styles . inputLabel }
63
+ value = { password . value }
64
+ onChangeText = { ( value ) => setPassword ( { ...password , value : value } ) }
65
+ errorMessage = { password . errorMessage }
66
+ secureTextEntry
67
+ />
68
+
29
69
< Button
30
70
color = "primary"
31
- title = { "Register " }
71
+ title = { "Login " }
32
72
size = "lg"
33
73
containerStyle = { { width : 200 , borderRadius : 5 } }
74
+ onPress = { handleLoginPress }
34
75
/>
35
76
</ View >
36
77
< Text style = { { paddingVertical : 10 } } >
37
- Already have an account?{ " " }
38
- < Text style = { { color : "blue" , fontWeight : "bold" } } onPress = { ( ) => { } } >
39
- Login Now
78
+ Don't have an account?{ " " }
79
+ < Text
80
+ style = { { color : "blue" , fontWeight : "bold" } }
81
+ onPress = { handleRegisterNowPress }
82
+ >
83
+ Register Now
40
84
</ Text >
41
85
</ Text >
42
86
</ ScrollView >
0 commit comments