Created
January 17, 2023 10:10
-
-
Save SahanAmarsha/496a2067ec6d9590c2f2fd6985fa1801 to your computer and use it in GitHub Desktop.
Add 'signIn()' to SignIn.tsx page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. | |
. | |
import useAuth from "../hooks/useAuth"; | |
import { useEffect } from "react"; | |
import LoadingSpinner from "../components/LoadingSpinner"; | |
export default function SignIn() { | |
const navigate = useNavigate(); | |
const { signIn, isAuthenticated, isAuthenticating } = useAuth(); | |
. | |
. | |
useEffect(() => { | |
if (isAuthenticated) { | |
navigate("/", { replace: true }); | |
} | |
}, [isAuthenticated]); | |
if (isAuthenticating || isAuthenticated) { | |
return <LoadingSpinner />; | |
} | |
return ( | |
<Container component="main" maxWidth="xs"> | |
<CssBaseline /> | |
<Box | |
sx={{ | |
marginTop: 8, | |
display: "flex", | |
flexDirection: "column", | |
alignItems: "center", | |
}} | |
> | |
<Avatar sx={{ m: 1, bgcolor: "secondary.main" }}> | |
<LockOutlinedIcon /> | |
</Avatar> | |
<Typography component="h1" variant="h5"> | |
Sign in | |
</Typography> | |
<Formik | |
initialValues={{ | |
email: "", | |
password: "", | |
submit: null, | |
}} | |
validationSchema={validationSchema} | |
onSubmit={async ( | |
values: any, | |
{ setErrors, setStatus, setSubmitting }: any | |
): Promise<void> => { | |
try { | |
setSubmitting(true); | |
// user sign in | |
await signIn({email: values.email, password: values.password}); | |
navigate("/", { replace: true }); | |
setSubmitting(false); | |
} catch (err: any) { | |
setStatus({ success: false }); | |
setErrors({ submit: err.message }); | |
setSubmitting(false); | |
} | |
}} | |
> | |
{({ | |
errors, | |
handleChange, | |
handleSubmit, | |
isSubmitting, | |
touched, | |
values, | |
}): JSX.Element => ( | |
<Box | |
component="form" | |
onSubmit={handleSubmit} | |
noValidate | |
sx={{ mt: 1 }} | |
> | |
. | |
. | |
</Box> | |
)} | |
</Formik> | |
</Box> | |
<Copyright sx={{ mt: 8, mb: 4 }} /> | |
</Container> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment