|
| 1 | +import { useForm } from "react-hook-form" |
| 2 | +import { useRouter } from "next/router" |
| 3 | +import { |
| 4 | + useSignInWithEmailAndPassword, |
| 5 | + SignInWithEmailAndPasswordData |
| 6 | +} from "../auth/hooks" |
| 7 | +import { Form, Stack, Alert, Container, Row, Col, Card } from "../bootstrap" |
| 8 | +import Input from "../forms/Input" |
| 9 | +import PasswordInput from "../forms/PasswordInput" |
1 | 10 | import { useTranslation } from "next-i18next"
|
2 |
| -import styled from "styled-components" |
3 |
| -import { Container } from "../bootstrap" |
4 |
| -import { SignInWithButton } from "components/auth" |
| 11 | +import { LoadingButton } from "../buttons" |
| 12 | +import SocialSignOnButtons from "components/auth/SocialSignOnButtons" |
| 13 | +import Divider from "../Divider/Divider" |
5 | 14 |
|
6 |
| -export default function LoginPage() { |
| 15 | +export default function Login() { |
| 16 | + const router = useRouter() |
| 17 | + const redirect = (router.query.redirect as string) || "/" |
7 | 18 | const { t } = useTranslation("auth")
|
8 | 19 |
|
9 |
| - const StyledContainer = styled(Container)` |
10 |
| - @media (min-width: 768px) { |
11 |
| - } |
12 |
| - ` |
| 20 | + const { |
| 21 | + register, |
| 22 | + handleSubmit, |
| 23 | + formState: { errors } |
| 24 | + } = useForm<SignInWithEmailAndPasswordData>() |
| 25 | + |
| 26 | + const signIn = useSignInWithEmailAndPassword() |
| 27 | + |
| 28 | + const success = () => { |
| 29 | + const safeRedirect = redirect.startsWith("/") ? redirect : "/" |
| 30 | + router.replace(safeRedirect) |
| 31 | + } |
| 32 | + const onSubmit = handleSubmit(credentials => { |
| 33 | + signIn.execute(credentials).then(success) |
| 34 | + }) |
13 | 35 |
|
14 | 36 | return (
|
15 |
| - <StyledContainer> |
16 |
| - <div className={`bg-white my-3 overflow-hidden rounded-3`}> |
17 |
| - <div |
18 |
| - className={`align-items-center d-flex justify-content-center px-2 pt-2 pb-0`} |
19 |
| - > |
20 |
| - <div className={`px-3 py-0`}> |
21 |
| - <div |
22 |
| - className={`align-items-center d-flex fs-5 justify-content-center lh-sm mb-1 text-secondary`} |
23 |
| - > |
24 |
| - <div className={`pe-2`}> |
25 |
| - <img |
26 |
| - alt="closed lock with key" |
27 |
| - src={`/closed-lock-with-key.png`} |
28 |
| - width="32" |
29 |
| - height="32" |
30 |
| - />{" "} |
31 |
| - </div> |
32 |
| - {t("almostThere")} |
33 |
| - </div> |
34 |
| - <br /> |
35 |
| - <div |
36 |
| - className={`align-items-start d-flex fs-5 justify-content-center lh-sm mb-1 text-secondary`} |
37 |
| - > |
38 |
| - {t("justLogIn")} |
39 |
| - </div> |
40 |
| - <br /> |
41 |
| - <div className={`justify-content-center d-flex w-100`}> |
42 |
| - <SignInWithButton /> |
43 |
| - </div> |
44 |
| - <br /> |
45 |
| - </div> |
46 |
| - </div> |
47 |
| - </div> |
48 |
| - </StyledContainer> |
| 37 | + <Container className="py-5"> |
| 38 | + <Row className="justify-content-center"> |
| 39 | + <Col xs={12} sm={10} md={8} lg={6}> |
| 40 | + <Card className="p-4 shadow-lg"> |
| 41 | + <h2 className="text-center">{t("signInToAccess")}</h2> |
| 42 | + |
| 43 | + <Form onSubmit={onSubmit} noValidate> |
| 44 | + {signIn.error && ( |
| 45 | + <Alert variant="danger">{signIn.error.message}</Alert> |
| 46 | + )} |
| 47 | + |
| 48 | + <Stack gap={3}> |
| 49 | + <Input |
| 50 | + label={t("email")} |
| 51 | + type="email" |
| 52 | + {...register("email", { |
| 53 | + required: t("emailIsRequired") ?? "Email is required" |
| 54 | + })} |
| 55 | + error={errors.email?.message} |
| 56 | + /> |
| 57 | + |
| 58 | + <PasswordInput |
| 59 | + label={t("password")} |
| 60 | + {...register("password", { |
| 61 | + required: t("passwordRequired") ?? "Password is required" |
| 62 | + })} |
| 63 | + error={errors.password?.message} |
| 64 | + /> |
| 65 | + |
| 66 | + <LoadingButton |
| 67 | + type="submit" |
| 68 | + className="w-100" |
| 69 | + loading={signIn.loading} |
| 70 | + > |
| 71 | + {t("signIn")} |
| 72 | + </LoadingButton> |
| 73 | + |
| 74 | + <Divider className="px-4">{t("or")}</Divider> |
| 75 | + |
| 76 | + <SocialSignOnButtons |
| 77 | + onComplete={() => router.replace(redirect)} |
| 78 | + /> |
| 79 | + </Stack> |
| 80 | + </Form> |
| 81 | + </Card> |
| 82 | + </Col> |
| 83 | + </Row> |
| 84 | + </Container> |
49 | 85 | )
|
50 | 86 | }
|
0 commit comments