Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling issue #37 #39

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 93 additions & 54 deletions src/Screens/LoginScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,94 +1,133 @@
import React, { useCallback } from "react";
import { View, StyleSheet, Keyboard, KeyboardAvoidingView, Platform, TouchableWithoutFeedback } from "react-native";
import React, { useState, useCallback } from "react";
import {
View,
StyleSheet,
Keyboard,
KeyboardAvoidingView,
Platform,
TouchableWithoutFeedback,
Text,
} from "react-native";
import { useDispatch } from "react-redux";
import { auth, signInWithEmailAndPassword } from "../firebase-utilities";
import { setLoading, setUser } from "../state-managment/reducers";
import { useForm, SubmitHandler } from "react-hook-form"
import { Text, Button } from "react-native-paper"
import { useForm, SubmitHandler, set } from "react-hook-form";
import { TextInput } from "@/components/input/TextInput";
import { Button } from "react-native-paper";

type LoginFormData = {
email: string;
password: string;
}
};

export const useLoginScreen = () => {
const dispatch = useDispatch();
const [error, setError] = useState<string | null>(null);

const {
control,
handleSubmit,
formState: { errors },
} = useForm<LoginFormData>({
defaultValues: {
email: "",
password: "",
},
})

const onLoginFormSubmit: SubmitHandler<LoginFormData> = useCallback(async (data) => {
dispatch(setLoading(true));
const { email, password } = data
});

try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
dispatch(setUser(userCredential.user));
} catch (error) {
console.error(error.message);
dispatch(setLoading(false));
}
}, [dispatch, signInWithEmailAndPassword])
const onLoginFormSubmit: SubmitHandler<LoginFormData> = useCallback(
async (data) => {
setError(null);
try {
const userCredential = await signInWithEmailAndPassword(
auth,
data.email,
data.password
);
dispatch(setUser(userCredential.user));
} catch (error: any) {
console.error(error.message);
if (error.code) {
switch (error.code) {
case "auth/invalid-email":
setError("The email address is badly formatted.");
break;
case "auth/wrong-password":
setError("Wrong password. Please try again.");
break;
case "auth/user-not-found":
setError("No user found with this email.");
break;
default:
setError("An unexpected error occurred. Please try again.");
break;
}
} else {
setError("A network error occurred. Please try again later.");
}
}
},
[dispatch]
);

return {
control,
handleSubmit,
onLoginFormSubmit
}
}
onLoginFormSubmit,
error,
};
};

export const LoginScreen = () => {
const { control,
handleSubmit,
onLoginFormSubmit } = useLoginScreen();
const { control, handleSubmit, onLoginFormSubmit, error } = useLoginScreen();

return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={{ flex: 1 }}
>
<TouchableWithoutFeedback onPress={Keyboard.dismiss}>
<View style={styles.container}>
<View>
<Text style={{
fontSize: 36,
fontWeight: "bold"
}}>Welcome Back!</Text>
<Text variant="bodySmall">We're glad to see you back.</Text>
<Text style={{ fontSize: 36, fontWeight: "bold" }}>
Welcome Back!
</Text>
<Text>We're glad to see you back.</Text>
</View>

<View style={{
marginVertical: 50,
gap: 8
}}>
<TextInput control={control} name="email" label={"Your Email"} rules={{
required: {
value: true,
message: "Please enter your email address",
},
}} />

<TextInput secureTextEntry control={control} name="password" label={"Your Password"} rules={{
required: {
value: true,
message: "Please enter your password",
},
}} />
<View style={{ marginVertical: 50, gap: 8 }}>
<TextInput
control={control}
name="email"
label={"Your Email"}
rules={{
required: {
value: true,
message: "Please enter your email address",
},
}}
/>
<TextInput
secureTextEntry
control={control}
name="password"
label={"Your Password"}
rules={{
required: {
value: true,
message: "Please enter your password",
},
}}
/>
{error && (
<Text style={{ color: "red", marginVertical: 8 }}>{error}</Text>
)}
</View>

<View style={{
gap: 8
}}>
<Button mode="contained" onPress={handleSubmit(onLoginFormSubmit)}>Login</Button>
<View style={{ gap: 8 }}>
<Button mode="contained" onPress={handleSubmit(onLoginFormSubmit)}>
Login
</Button>
</View>

</View>
</TouchableWithoutFeedback>
</KeyboardAvoidingView>
Expand All @@ -102,5 +141,5 @@ const styles = StyleSheet.create({
paddingVertical: 75,
paddingLeft: 24,
paddingRight: 24,
}
},
});