From 320e26e6277ddea40b92ea9fd9eb1bdfb2467600 Mon Sep 17 00:00:00 2001 From: Maruf Bepary Date: Mon, 30 Jan 2023 21:11:21 +0000 Subject: [PATCH] Implemented signup using email and password --- src/components/Modal/Auth/Signup.tsx | 88 ++++++++++++++++++++++------ src/firebase/errors.ts | 14 +++++ 2 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 src/firebase/errors.ts diff --git a/src/components/Modal/Auth/Signup.tsx b/src/components/Modal/Auth/Signup.tsx index 41b37d0..8dcb62a 100644 --- a/src/components/Modal/Auth/Signup.tsx +++ b/src/components/Modal/Auth/Signup.tsx @@ -1,27 +1,61 @@ -import { authModalState } from '@/atoms/authModalAtom'; -import { Input, Button, Flex, Text } from '@chakra-ui/react'; -import React, { useState } from 'react'; -import { useSetRecoilState } from 'recoil'; +import { Input, Button, Flex, Text } from "@chakra-ui/react"; +import React, { useState } from "react"; +import { useSetRecoilState } from "recoil"; +import { authModalState } from "../../../atoms/authModalAtom"; +import { auth } from "../../../firebase/clientApp"; +import { FIREBASE_ERRORS } from "../../../firebase/errors"; +import { useCreateUserWithEmailAndPassword } from "react-firebase-hooks/auth"; -const Signup:React.FC = () => { - const setAuthModalState = useSetRecoilState(authModalState); - const [signUpForm, setSignUpForm] = useState({ +const SignUp = () => { + const setaAuthModalState = useSetRecoilState(authModalState); // Set global state + const [signUpForm, setSignUpForm] = useState({ email: "", // Initially empty email password: "", // Initially empty password - conformPassword: "", + confirmPassword: "", // Initially empty confirm password }); + const [error, setError] = useState(""); // Initially empty error + const [ + createUserWithEmailAndPassword, // returns a function that returns the user, loading or error + user, + loading, + userError, + ] = useCreateUserWithEmailAndPassword(auth); - const onSubmit = () => {}; + /** + * This function is used as the event handler for a form submission. + * It will prevent the page from refreshing. + * Checks if the password and confirm password fields match. + * If they do not match, an error message is set and the function returns without creating a new user. + * If the passwords match, a new user is created using the email and password provided in the form. + * @param event (React.FormEvent): the submit event triggered by the form + * @returns None + */ + const onSubmit = (event: React.FormEvent) => { + event.preventDefault(); // Prevent the page from refreshing + if (error) setError(""); // If there is an error, clear it + if (signUpForm.password !== signUpForm.confirmPassword) { + // If the password and confirm password don't match + setError("Passwords don't match"); // Set error + return; // Return so that the function doesn't continue + } + createUserWithEmailAndPassword(signUpForm.email, signUpForm.password); // Create user with email and password + }; // Function to execute when the form is submitted + /** + * Function to execute when the form is changed (when email and password are typed). + * Multiple inputs use the same onChange function. + * @param event(React.ChangeEvent) - the event that is triggered when the form is changed + */ const onChange = (event: React.ChangeEvent) => { + // Update form state setSignUpForm((prev) => ({ - ...prev, - [event.target.name]: event.target.value, + ...prev, // Spread previous state because we don't want to lose the other input's value + [event.target.name]: event.target.value, // Catch the name of the input that was changed and update the corresponding state })); }; return ( -
+ { border: "1px solid", }} /> + { border: "1px solid", }} /> - { border: "1px solid", }} /> - @@ -99,7 +152,7 @@ const Signup:React.FC = () => { fontWeight={700} cursor="pointer" onClick={() => - setAuthModalState((prev) => ({ + setaAuthModalState((prev) => ({ ...prev, view: "login", })) @@ -110,5 +163,6 @@ const Signup:React.FC = () => {
); -} -export default Signup; \ No newline at end of file +}; + +export default SignUp; diff --git a/src/firebase/errors.ts b/src/firebase/errors.ts new file mode 100644 index 0000000..902f731 --- /dev/null +++ b/src/firebase/errors.ts @@ -0,0 +1,14 @@ +/** + * Firebase error messages. + * The default error messages are not very user friendly hence they are mapped to more user friendly messages. + * @see https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithEmailAndPassword + */ +export const FIREBASE_ERRORS = { + // Sign Up Errors + "Firebase: Error (auth/email-already-in-use).": "Email already in use.", + "Firebase: Error (auth/account-exists-with-different-credential).": "Email already in use.", + + // Sign In Errors + "Firebase: Error (auth/user-not-found).": "Invalid email or password", + "Firebase: Error (auth/wrong-password).": "Invalid email or password", +} \ No newline at end of file