Skip to content

Commit 65ce0ab

Browse files
committed
Solved the login component duplicated issues
1 parent 4afa85d commit 65ce0ab

File tree

3 files changed

+99
-38
lines changed

3 files changed

+99
-38
lines changed

components/Login/login.tsx

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

pages/login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createPage } from "../components/page"
2-
import Login from "components/Login/Login"
2+
import Login from "components/Login/login"
33

44
export default createPage({
55
title: "Login",

public/locales/en/auth.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@
5656
"verifyLinkSent": "Please verify your email for your account by clicking the verification link we sent to your email. You will be required to verify your email before submitting testimony.",
5757
"setUpProfile": "Set Up Your Profile",
5858
"probablySignedOut": "You were possibly signed out while trying to go to a page that needs to be signed in to function",
59-
"pleaseConsider": "Please consider logging in first:"
59+
"pleaseConsider": "Please consider logging in first:",
60+
"signInToAccess": "Login required to access the contents"
6061
}

0 commit comments

Comments
 (0)