Skip to content

Commit

Permalink
Added reset password functionality
Browse files Browse the repository at this point in the history
- Display reset password option on log in modal only
- Display `resetPassword` modal only when state is not login or signup
- Use Firebase hook to send email with reset instructions
  • Loading branch information
mbeps committed Jan 31, 2023
1 parent 8ca5b76 commit 584c35e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/components/Modal/Auth/AuthModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useAuthState } from "react-firebase-hooks/auth";
import { useRecoilState } from "recoil";
import AuthInputs from "./AuthInputs";
import OAuthButtons from "./OAuthButtons";
import ResetPassword from "./ResetPassword";

const AuthModal: React.FC = () => {
const [modalState, setModalState] = useRecoilState(authModalState);
Expand Down Expand Up @@ -57,11 +58,16 @@ const AuthModal: React.FC = () => {
justify="center"
width="70%"
>
<OAuthButtons />
{/* <Text color='gray.500' fontWeight={700}>OR</Text> */}
<Divider />
<AuthInputs />
{/* <ResetPassword /> */}
{modalState.view === "login" || modalState.view === "signup" ? (
<>
<OAuthButtons />
{/* <Text color='gray.500' fontWeight={700}>OR</Text> */}
<Divider />
<AuthInputs />
</>
) : (
<ResetPassword />
)}
</Flex>
</ModalBody>
</ModalContent>
Expand Down
28 changes: 27 additions & 1 deletion src/components/Modal/Auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,38 @@ const Login: React.FC<LoginProps> = () => {
{FIREBASE_ERRORS[error?.message as keyof typeof FIREBASE_ERRORS]}
</Text>

<Button width="100%" height="36px" mt={2} mb={2} type="submit" isLoading={loading}>
<Button
width="100%"
height="36px"
mt={2}
mb={2}
type="submit"
isLoading={loading}
>
{" "}
{/* When the form is submitted, execute onSubmit function */}
Log In
</Button>

<Flex fontSize="9pt" justifyContent="center" mb={2}>
<Text fontSize="9pt" mr={1}>
Forgot your password?
</Text>
<Text
color="red.500"
fontWeight={700}
cursor="pointer"
onClick={() =>
setAuthModalState((prev) => ({
...prev,
view: "resetPassword",
}))
}
>
Reset Password
</Text>
</Flex>

<Flex fontSize="9pt" justifyContent="center">
<Text mr={1}>Want to join the circus? </Text>
<Text
Expand Down
107 changes: 107 additions & 0 deletions src/components/Modal/Auth/ResetPassword.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Button, Flex, Icon, Image, Input, Text } from "@chakra-ui/react";
import React, { useState } from "react";
import { useSendPasswordResetEmail } from "react-firebase-hooks/auth";
import { BsDot } from "react-icons/bs";
import { useSetRecoilState } from "recoil";
import { authModalState } from "../../../atoms/authModalAtom";
import { auth } from "../../../firebase/clientApp";

const ResetPassword: React.FC = () => {
const setAuthModalState = useSetRecoilState(authModalState);
const [email, setEmail] = useState("");
const [success, setSuccess] = useState(false);
const [sendPasswordResetEmail, sending, error] =
useSendPasswordResetEmail(auth);

const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();

await sendPasswordResetEmail(email);
setSuccess(true);
};
return (
<Flex direction="column" alignItems="center" width="100%">
<Image src="/images/logo.svg" height="40px" mb={2} alt="Website logo"/>
<Text fontWeight={700} mb={2}>
Reset your password
</Text>
{success ? (
<Text mb={4}>Check your email</Text>
) : (
<>
<Text fontSize="sm" textAlign="center" mb={2}>
Enter the email associated with your account and we will send you a
reset link
</Text>
<form onSubmit={onSubmit} style={{ width: "100%" }}>
<Input
required
name="email"
placeholder="Email"
type="email"
mb={2}
onChange={(event) => setEmail(event.target.value)}
fontSize="10pt"
_placeholder={{ color: "gray.500" }}
_hover={{
bg: "white",
border: "1px solid",
borderColor: "blue.500",
}}
_focus={{
outline: "none",
bg: "white",
border: "1px solid",
borderColor: "blue.500",
}}
bg="gray.50"
/>
<Text textAlign="center" fontSize="10pt" color="red">
{error?.message}
</Text>
<Button
width="100%"
height="36px"
mb={2}
mt={2}
type="submit"
isLoading={sending}
>
Reset Password
</Button>
</form>
</>
)}
<Flex
alignItems="center"
fontSize="9pt"
color="red.500"
fontWeight={700}
cursor="pointer"
>
<Text
onClick={() =>
setAuthModalState((prev) => ({
...prev,
view: "login",
}))
}
>
LOGIN
</Text>
<Icon as={BsDot} />
<Text
onClick={() =>
setAuthModalState((prev) => ({
...prev,
view: "signup",
}))
}
>
SIGN UP
</Text>
</Flex>
</Flex>
);
};
export default ResetPassword;

0 comments on commit 584c35e

Please sign in to comment.