Skip to content

Commit 11ec628

Browse files
committed
[Update] Set up login
1 parent efc7516 commit 11ec628

File tree

10 files changed

+203
-25
lines changed

10 files changed

+203
-25
lines changed

package-lock.json

+50-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"web": "expo start --web"
1010
},
1111
"dependencies": {
12+
"@react-navigation/bottom-tabs": "^6.5.11",
1213
"@react-navigation/native": "^6.1.9",
1314
"@react-navigation/native-stack": "^6.9.17",
1415
"@rneui/base": "^4.0.0-rc.7",
1516
"@rneui/themed": "^4.0.0-rc.8",
16-
"axios": "^1.5.0",
17+
"axios": "^1.6.2",
1718
"eas": "^0.1.0",
1819
"expo": "~49.0.11",
1920
"expo-av": "~13.4.1",

src/constants/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ export const COLORS = {
1515
gray: "#808080",
1616
lightGray: "#D3D3D3",
1717
};
18+
19+
//TODO: Move following to env
20+
export const SERVER_URL = "http://13.51.86.179:5500";
21+
export const API_BASE_URL = SERVER_URL + "/api";

src/hooks/useAuth.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { View, Text } from "react-native";
2+
import { useState } from "react";
3+
import axios from "axios";
4+
import { API_BASE_URL } from "../constants";
5+
6+
const useAuth = () => {
7+
const [loading, setLoading] = useState(false);
8+
9+
async function register(userData) {
10+
setLoading(true);
11+
try {
12+
const signUpUrl = API_BASE_URL + "/signup";
13+
const user = await axios.post(signUpUrl, userData);
14+
console.log("User registered: ", user.data);
15+
} catch (error) {
16+
throw error;
17+
} finally {
18+
setLoading(false);
19+
}
20+
}
21+
22+
async function login(email, password) {
23+
setLoading(true);
24+
try {
25+
const loginUrl = API_BASE_URL + "/login";
26+
const loginData = { email: email, password: password };
27+
const user = await axios.post(loginUrl, loginData);
28+
console.log("Successfully logged in: ", user);
29+
} catch (error) {
30+
throw error;
31+
} finally {
32+
setLoading(false);
33+
}
34+
}
35+
36+
return { register, login };
37+
};
38+
39+
export default useAuth;

src/routes/AuthStack.jsx

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const AuthStackScreen = () => {
2020
options={{ headerShown: false }}
2121
/>
2222
<AuthStack.Screen name="Register" component={RegisterScreen} />
23-
2423
</AuthStack.Navigator>
2524
);
2625
};

src/routes/HomeStack.jsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { View, Text } from "react-native";
2+
import React from "react";
3+
import { createNativeStackNavigator } from "@react-navigation/native-stack";
4+
import HomeScreen from "../screens/home/HomeScreen";
5+
6+
const HomeStack = createNativeStackNavigator();
7+
8+
const HomeStackScreen = () => {
9+
return (
10+
<HomeStack.Navigator
11+
screenOptions={{
12+
animation: "slide_from_right",
13+
}}
14+
>
15+
<HomeStack.Screen
16+
name="Home"
17+
component={HomeScreen}
18+
options={{ headerShown: false }}
19+
/>
20+
</HomeStack.Navigator>
21+
);
22+
};
23+
24+
export default HomeStackScreen;

src/routes/TabNavigation.jsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { View, Text } from "react-native";
22
import React from "react";
3+
import { NavigationContainer } from "@react-navigation/native";
4+
import { createBottomTabNavigator } from "@react-navigation/bottom-tabs";
5+
import HomeStackScreen from "./HomeStack";
36

7+
const Tab = createBottomTabNavigator();
48
const TabNavigation = () => {
59
return (
6-
<View>
7-
<Text>TabNavigation</Text>
8-
</View>
10+
<NavigationContainer>
11+
<Tab.Navigator>
12+
<Tab.Screen name="Home" component={HomeStackScreen} />
13+
</Tab.Navigator>
14+
</NavigationContainer>
915
);
1016
};
1117

src/screens/auth/LoginScreen.jsx

+54-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
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 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
714
const handleRegisterNowPress = () => {
815
navigation.navigate("Register");
916
};
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+
1038
return (
1139
<ScrollView
1240
style={{ width: "100%" }}
@@ -18,25 +46,41 @@ const Login = ({ navigation }) => {
1846
paddingHorizontal: 30,
1947
}}
2048
>
21-
<Text h2>Register</Text>
49+
<Text h2>Login</Text>
2250
<View
2351
style={{ width: "100%", alignItems: "center", paddingVertical: 40 }}
2452
>
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+
2969
<Button
3070
color="primary"
31-
title={"Register"}
71+
title={"Login"}
3272
size="lg"
3373
containerStyle={{ width: 200, borderRadius: 5 }}
74+
onPress={handleLoginPress}
3475
/>
3576
</View>
3677
<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
4084
</Text>
4185
</Text>
4286
</ScrollView>

src/screens/auth/RegisterScreen.jsx

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import React from "react";
33
import { Button, Input, Text } from "@rneui/themed";
44
import { COLORS } from "../../constants";
55

6-
const Register = () => {
6+
const Register = ({ navigation }) => {
7+
const handleLoginPress = () => {
8+
navigation.navigate("Login");
9+
};
710
return (
811
<ScrollView
912
style={{ width: "100%" }}
@@ -15,26 +18,30 @@ const Register = () => {
1518
paddingHorizontal: 30,
1619
}}
1720
>
18-
<Text h2>Login</Text>
21+
<Text h2>Register</Text>
1922
<View
2023
style={{ width: "100%", alignItems: "center", paddingVertical: 40 }}
2124
>
25+
<Input label={"First Name"} labelStyle={styles.inputLabel} />
26+
<Input label={"Last Name"} labelStyle={styles.inputLabel} />
2227
<Input label={"Email"} labelStyle={styles.inputLabel} />
2328
<Input label={"Password"} labelStyle={styles.inputLabel} />
29+
<Input label={"Confirm Password"} labelStyle={styles.inputLabel} />
30+
2431
<Button
2532
color="primary"
26-
title={"Login"}
33+
title={"Register"}
2734
size="lg"
2835
containerStyle={{ width: 200, borderRadius: 5 }}
2936
/>
3037
</View>
3138
<Text style={{ paddingVertical: 10 }}>
32-
Dont have an account?{" "}
39+
Already have an account?{" "}
3340
<Text
3441
style={{ color: "blue", fontWeight: "bold" }}
35-
onPress={handleRegisterNowPress}
42+
onPress={handleLoginPress}
3643
>
37-
Register Now
44+
Login
3845
</Text>
3946
</Text>
4047
</ScrollView>

src/screens/home/HomeScreen.jsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { View, Text } from "react-native";
2+
import React from "react";
3+
4+
const HomeScreen = () => {
5+
return <View></View>;
6+
};
7+
8+
export default HomeScreen;

0 commit comments

Comments
 (0)