Skip to content

Commit f094324

Browse files
committed
[Update] Complete registeration
1 parent 11ec628 commit f094324

File tree

2 files changed

+134
-8
lines changed

2 files changed

+134
-8
lines changed

src/hooks/useAuth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const useAuth = () => {
1111
try {
1212
const signUpUrl = API_BASE_URL + "/signup";
1313
const user = await axios.post(signUpUrl, userData);
14-
console.log("User registered: ", user.data);
14+
return user.data;
1515
} catch (error) {
1616
throw error;
1717
} finally {

src/screens/auth/RegisterScreen.jsx

+133-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,103 @@
11
import { View, StyleSheet, ScrollView } from "react-native";
2-
import React from "react";
2+
import { useState } from "react";
33
import { Button, Input, Text } from "@rneui/themed";
44
import { COLORS } from "../../constants";
5+
import useAuth from "../../hooks/useAuth";
56

67
const Register = ({ navigation }) => {
8+
// Variables
9+
const { register, loading } = useAuth();
10+
const [firstName, setFirstName] = useState({
11+
value: null,
12+
errorMessage: null,
13+
});
14+
const [lastName, setLastName] = useState({ value: null, errorMessage: null });
15+
const [email, setEmail] = useState({ value: null, errorMessage: null });
16+
const [password, setPassword] = useState({ value: null, errorMessage: null });
17+
const [confirmPassword, setConfirmPassword] = useState({
18+
value: null,
19+
errorMessage: null,
20+
});
21+
22+
// Event handlers
723
const handleLoginPress = () => {
824
navigation.navigate("Login");
925
};
26+
const handleRegisterPress = async () => {
27+
setFirstName({ ...firstName, errorMessage: null });
28+
setLastName({ ...lastName, errorMessage: null });
29+
setEmail({ ...email, errorMessage: null });
30+
setPassword({ ...password, errorMessage: null });
31+
setConfirmPassword({ ...confirmPassword, errorMessage: null });
32+
if (!firstName.value) {
33+
setFirstName({
34+
...firstName,
35+
errorMessage: "Please fill out all fields.",
36+
});
37+
}
38+
if (!lastName.value) {
39+
setLastName({
40+
...lastName,
41+
errorMessage: "Please fill out all fields.",
42+
});
43+
}
44+
if (!email.value) {
45+
setEmail({ ...email, errorMessage: "Please fill out all fields." });
46+
}
47+
if (!password.value) {
48+
setPassword({
49+
...password,
50+
errorMessage: "Please fill out all fields.",
51+
});
52+
}
53+
if (!confirmPassword.value) {
54+
setConfirmPassword({
55+
...confirmPassword,
56+
errorMessage: "Please fill out all fields.",
57+
});
58+
}
59+
60+
if (
61+
!(
62+
firstName.value &&
63+
lastName.value &&
64+
email.value &&
65+
password.value &&
66+
confirmPassword.value
67+
)
68+
) {
69+
return;
70+
}
71+
72+
if (password.value != confirmPassword.value) {
73+
setPassword({
74+
...password,
75+
errorMessage: "Password and confirm password do not match.",
76+
});
77+
setConfirmPassword({
78+
...confirmPassword,
79+
errorMessage: "Password and confirm password do not match.",
80+
});
81+
return;
82+
}
83+
84+
try {
85+
const userData = {
86+
firstName: firstName.value,
87+
lastName: lastName.value,
88+
email: email.value,
89+
password: password.value,
90+
confirmPassword: confirmPassword.value,
91+
};
92+
const user = await register(userData);
93+
navigation.navigate("Login");
94+
} catch (error) {
95+
console.error(
96+
`Error while registering user: Status: ${error.response?.status} Message: ${error.response?.data?.message}
97+
`
98+
);
99+
}
100+
};
10101
return (
11102
<ScrollView
12103
style={{ width: "100%" }}
@@ -22,17 +113,52 @@ const Register = ({ navigation }) => {
22113
<View
23114
style={{ width: "100%", alignItems: "center", paddingVertical: 40 }}
24115
>
25-
<Input label={"First Name"} labelStyle={styles.inputLabel} />
26-
<Input label={"Last Name"} labelStyle={styles.inputLabel} />
27-
<Input label={"Email"} labelStyle={styles.inputLabel} />
28-
<Input label={"Password"} labelStyle={styles.inputLabel} />
29-
<Input label={"Confirm Password"} labelStyle={styles.inputLabel} />
116+
<Input
117+
label={"First Name"}
118+
labelStyle={styles.inputLabel}
119+
value={firstName.value}
120+
errorMessage={firstName.errorMessage}
121+
onChangeText={(value) => setFirstName({ ...firstName, value })}
122+
/>
123+
<Input
124+
label={"Last Name"}
125+
labelStyle={styles.inputLabel}
126+
value={lastName.value}
127+
errorMessage={lastName.errorMessage}
128+
onChangeText={(value) => setLastName({ ...lastName, value })}
129+
/>
130+
<Input
131+
label={"Email"}
132+
labelStyle={styles.inputLabel}
133+
value={email.value}
134+
errorMessage={email.errorMessage}
135+
onChangeText={(value) => setEmail({ ...email, value })}
136+
/>
137+
<Input
138+
label={"Password"}
139+
labelStyle={styles.inputLabel}
140+
value={password.value}
141+
errorMessage={password.errorMessage}
142+
onChangeText={(value) => setPassword({ ...password, value })}
143+
secureTextEntry
144+
/>
145+
<Input
146+
label={"Confirm Password"}
147+
labelStyle={styles.inputLabel}
148+
value={confirmPassword.value}
149+
errorMessage={confirmPassword.errorMessage}
150+
onChangeText={(value) =>
151+
setConfirmPassword({ ...confirmPassword, value })
152+
}
153+
secureTextEntry
154+
/>
30155

31156
<Button
32157
color="primary"
33158
title={"Register"}
34159
size="lg"
35-
containerStyle={{ width: 200, borderRadius: 5 }}
160+
containerStyle={{ width: 200, borderRadius: 5, marginTop: 20 }}
161+
onPress={handleRegisterPress}
36162
/>
37163
</View>
38164
<Text style={{ paddingVertical: 10 }}>

0 commit comments

Comments
 (0)