-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSignIn.js
More file actions
147 lines (135 loc) · 4.23 KB
/
SignIn.js
File metadata and controls
147 lines (135 loc) · 4.23 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import {
GoogleAuthProvider,
signInWithPopup,
signInWithEmailAndPassword,
onAuthStateChanged,
} from "firebase/auth";
import { auth } from "../firebase";
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import FormContainer from "../components/Form/FormContainer";
import { Button, VStack, HStack, useColorMode } from "@chakra-ui/react";
import YupValidation, { initialValues } from "../components/Form/YupSignIn";
import TextField from "../components/Form/TextField";
import { Formik, Form } from "formik";
import { IconContext } from "react-icons";
import { FiLogIn } from "react-icons/fi";
const getAuthErrorMessage = (error) => {
switch (error.code) {
case 'auth/user-not-found':
case 'auth/wrong-password':
case 'auth/invalid-credential':
return 'Invalid email or password. Please try again.';
case 'auth/invalid-email':
return 'Please enter a valid email address.';
case 'auth/too-many-requests':
return 'Too many attempts. Please try again later or reset your password.';
case 'auth/popup-closed-by-user':
case 'auth/cancelled-popup-request':
return 'The sign-in process was cancelled.';
case 'auth/popup-blocked':
return 'Pop-up blocked. Please allow pop-ups for this site to sign in.';
default:
return 'An unexpected error occurred. Please try again.';
}
};
export default function Signin() {
const Navigate = useNavigate();
const { colorMode } = useColorMode();
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
Navigate("/main");
}
});
return () => unsubscribe();
}, [Navigate]);
const NavToSignUp = () => {
Navigate("/signup");
};
const SignInWithGoogle = () => {
const provider = new GoogleAuthProvider();
signInWithPopup(auth, provider)
.then(() => {
toast.success("Successfully logged in!");
})
.catch((err) => {
const message = getAuthErrorMessage(err);
toast.error(message);
console.error("Firebase Google Auth Error:", err);
});
};
const SignInWithEmailPassword = (values, actions) => {
signInWithEmailAndPassword(auth, values.email, values.password)
.then(() => {
toast.success("Successfully logged in!");
actions.setSubmitting(false);
})
.catch((err) => {
const message = getAuthErrorMessage(err);
toast.error(message);
actions.setSubmitting(false);
console.error("Firebase Email Auth Error:", err);
});
};
return (
<FormContainer Icon={LoginIcon} title="Sign in to your account!">
<ToastContainer position="top-right" autoClose={5000} hideProgressBar={false} />
<Formik
initialValues={initialValues}
validationSchema={YupValidation}
onSubmit={SignInWithEmailPassword}
>
{(props) => (
<Form>
<TextField
name="email"
type="email"
title="Email"
YupValidation={YupValidation}
/>
<TextField
name="password"
type="password"
title="Password"
YupValidation={YupValidation}
/>
<VStack w={"full"} marginTop="2">
<HStack w={"full"}>
<Button type="submit" isLoading={props.isSubmitting} w={"full"}>
Log In
</Button>
<Button
type="reset"
w="full"
bgColor={colorMode === "light" ? "red.700" : "red.400"}
>
Reset
</Button>
</HStack>
</VStack>
</Form>
)}
</Formik>
<Button onClick={NavToSignUp} w={"full"}>
Sign Up
</Button>
<Button onClick={SignInWithGoogle} w={"full"}>
Sign in with Google
</Button>
</FormContainer>
);
}
function LoginIcon() {
return (
<IconContext.Provider
value={{ style: { color: "rgb(211, 127, 16)", fontSize: "4rem" } }}
>
<div>
<FiLogIn />
</div>
</IconContext.Provider>
);
}