-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSignUp.js
More file actions
99 lines (90 loc) · 2.89 KB
/
SignUp.js
File metadata and controls
99 lines (90 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { useNavigate } from "react-router-dom";
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import FormContainer from "../components/Form/FormContainer";
import { VStack, Button, HStack } from "@chakra-ui/react";
import TextField from "../components/Form/TextField";
import { Formik, Form } from "formik";
import YupValidation, { initialValues } from "../components/Form/YupSignUp";
const getSignUpErrorMessage = (error) => {
switch (error.code) {
case 'auth/email-already-in-use':
return 'This email address is already taken.';
case 'auth/weak-password':
return 'The password is too weak. Please use at least 6 characters.';
case 'auth/invalid-email':
return 'Please enter a valid email address.';
default:
return 'An unexpected error occurred. Please try again.';
}
};
export default function SignUp() {
const auth = getAuth();
const Navigate = useNavigate();
const SignUp = (values, actions) => {
createUserWithEmailAndPassword(auth, values.email, values.password)
.then(() => {
toast.success("Account created successfully!", {
autoClose: 2000,
onClose: () => Navigate("/signin"),
});
actions.setSubmitting(false);
})
.catch((error) => {
const message = getSignUpErrorMessage(error);
toast.error(message);
actions.setSubmitting(false);
console.error("Firebase SignUp Error:", error);
});
};
const navToSignIn = () => {
Navigate("/signin");
};
return (
<FormContainer title="Sign up for an account!">
<ToastContainer />
<Formik
initialValues={initialValues}
validationSchema={YupValidation}
onSubmit={SignUp}
>
{(props) => (
<Form>
<TextField
name="email"
type="email"
title="Email"
YupValidation={YupValidation}
/>
<TextField
name="password"
type="password"
title="Password"
YupValidation={YupValidation}
/>
<TextField
name="confirmPassword"
type="password"
title="Confirm Password"
YupValidation={YupValidation}
/>
<VStack w={"full"} marginTop="2">
<HStack w={"full"}>
<Button type="submit" isLoading={props.isSubmitting} w={"full"}>
Sign Up
</Button>
<Button type="reset" w="full" bgColor="red.800">
Reset
</Button>
</HStack>
</VStack>
</Form>
)}
</Formik>
<Button onClick={navToSignIn} w={"full"}>
Sign In
</Button>
</FormContainer>
);
}