Skip to content

Commit 2080d2f

Browse files
committed
refactor(auth): update requireAuth to handle loading and redirect to login
1 parent c78328e commit 2080d2f

File tree

10 files changed

+88
-96
lines changed

10 files changed

+88
-96
lines changed

components/Login/Login.tsx

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
import { useForm } from "react-hook-form"
22
import { useRouter } from "next/router"
3-
import { useSignInWithEmailAndPassword, SignInWithEmailAndPasswordData } from "../auth/hooks"
4-
import { Form, Stack, Button, Alert } from "../bootstrap"
3+
import {
4+
useSignInWithEmailAndPassword,
5+
SignInWithEmailAndPasswordData
6+
} from "../auth/hooks"
7+
import {
8+
Form,
9+
Stack,
10+
Button,
11+
Alert,
12+
Container,
13+
Row,
14+
Col,
15+
Card
16+
} from "../bootstrap"
517
import Input from "../forms/Input"
618
import PasswordInput from "../forms/PasswordInput"
719
import { useTranslation } from "next-i18next"
@@ -17,7 +29,7 @@ export default function Login() {
1729
const {
1830
register,
1931
handleSubmit,
20-
formState: { errors },
32+
formState: { errors }
2133
} = useForm<SignInWithEmailAndPasswordData>()
2234

2335
const signIn = useSignInWithEmailAndPassword()
@@ -26,45 +38,58 @@ export default function Login() {
2638
const safeRedirect = redirect.startsWith("/") ? redirect : "/"
2739
router.replace(safeRedirect)
2840
}
29-
const onSubmit = handleSubmit( credentials => {
41+
const onSubmit = handleSubmit(credentials => {
3042
signIn.execute(credentials).then(success)
3143
})
3244

33-
3445
return (
35-
<Form onSubmit={onSubmit} noValidate>
36-
{signIn.error && <Alert variant="danger">{signIn.error.message}</Alert>}
46+
<Container className="py-5">
47+
<Row className="justify-content-center">
48+
<Col xs={12} sm={10} md={8} lg={6}>
49+
<Card className="p-4 shadow-lg">
50+
<h2 className="text-center mb-4">{t("signInToAccess")}</h2>
51+
52+
<Form onSubmit={onSubmit} noValidate>
53+
{signIn.error && (
54+
<Alert variant="danger">{signIn.error.message}</Alert>
55+
)}
3756

38-
<Stack gap={3}>
39-
<Input
40-
label={t("email")}
41-
type="email"
42-
{...register("email", {
43-
required: t("emailIsRequired") ?? "Email is required",
44-
})}
45-
error={errors.email?.message}
46-
/>
57+
<Stack gap={3}>
58+
<Input
59+
label={t("email")}
60+
type="email"
61+
{...register("email", {
62+
required: t("emailIsRequired") ?? "Email is required"
63+
})}
64+
error={errors.email?.message}
65+
/>
4766

48-
<PasswordInput
49-
label={t("password")}
50-
{...register("password", {
51-
required: t("passwordRequired") ?? "Password is required",
52-
})}
53-
error={errors.password?.message}
54-
/>
67+
<PasswordInput
68+
label={t("password")}
69+
{...register("password", {
70+
required: t("passwordRequired") ?? "Password is required"
71+
})}
72+
error={errors.password?.message}
73+
/>
5574

56-
<LoadingButton
57-
type="submit"
58-
className="w-100"
59-
loading={signIn.loading}
60-
>
61-
{t("signIn")}
62-
</LoadingButton>
75+
<LoadingButton
76+
type="submit"
77+
className="w-100"
78+
loading={signIn.loading}
79+
>
80+
{t("signIn")}
81+
</LoadingButton>
6382

64-
<Divider className="px-4">{t("or")}</Divider>
83+
<Divider className="px-4">{t("or")}</Divider>
6584

66-
<SocialSignOnButtons onComplete={onSubmit} />
67-
</Stack>
68-
</Form>
85+
<SocialSignOnButtons
86+
onComplete={() => router.replace(redirect)}
87+
/>
88+
</Stack>
89+
</Form>
90+
</Card>
91+
</Col>
92+
</Row>
93+
</Container>
6994
)
70-
}
95+
}

components/Newsfeed/Newsfeed.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ import {
1616
import ProfileSettingsModal from "components/EditProfilePage/ProfileSettingsModal"
1717
import { NewsfeedCard } from "components/NewsfeedCard/NewsfeedCard"
1818
import { ProfileButtons } from "components/ProfilePage/ProfileButtons"
19-
import { useAuthRedirect } from "components/auth/AuthRedirect"
2019

2120
export default function Newsfeed() {
2221
const { t } = useTranslation("common")
2322

24-
const { user, loading: authLoading } = useAuthRedirect()
23+
const { user } = useAuth()
2524
const uid = user?.uid
2625
const { result: profile, loading } = usePublicProfile(uid)
2726
const isUser = user?.uid !== undefined
@@ -192,7 +191,7 @@ export default function Newsfeed() {
192191

193192
return (
194193
<>
195-
{authLoading || loading ? (
194+
{loading ? (
196195
<Row>
197196
<Spinner animation="border" className="mx-auto" />
198197
</Row>

components/auth/AuthRedirect.tsx

Lines changed: 0 additions & 20 deletions
This file was deleted.

components/auth/ProtectedPageWrapper.tsx

Lines changed: 0 additions & 25 deletions
This file was deleted.

components/auth/Provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { getAuth, onAuthStateChanged, User } from "firebase/auth"
33
import { useDispatch } from "react-redux"
44
import { authChanged } from "./redux"
55

6-
export const Provider: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => {
6+
export const Provider: React.FC<React.PropsWithChildren<unknown>> = ({
7+
children
8+
}) => {
79
const dispatch = useDispatch()
810

911
useEffect(() => {

components/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from "./service"
33
export { default as SignInWithButton } from "./SignInWithButton"
44
export { default as SignOut } from "./SignOut"
55
export * from "./types"
6-
export { Provider } from "./Provider"
6+
export { Provider } from "./Provider"

components/auth/service.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useAppDispatch } from "../hooks"
88
import { createService } from "../service"
99
import { authChanged, useAuth } from "./redux"
1010
import { Claim } from "./types"
11+
import { Row, Spinner } from "../bootstrap"
1112

1213
export const { Provider } = createService(() => {
1314
const dispatch = useAppDispatch()
@@ -76,13 +77,22 @@ export function requireAuth(
7677
Component: React.FC<React.PropsWithChildren<{ user: User }>>
7778
) {
7879
return function ProtectedRoute() {
79-
const { user } = useAuth()
80+
const { user, loading } = useAuth()
8081
const router = useRouter()
8182
useEffect(() => {
82-
if (user === null) {
83-
router.push({ pathname: "/" })
83+
if (!loading && !user) {
84+
const currentPath = router.asPath
85+
router.replace(`/login?redirect=${encodeURIComponent(currentPath)}`)
8486
}
85-
}, [router, user])
87+
}, [user, loading, router])
88+
89+
if (loading) {
90+
return (
91+
<Row>
92+
<Spinner animation="border" className="mx-auto" />
93+
</Row>
94+
)
95+
}
8696

8797
return user ? <Component user={user} /> : null
8898
}
@@ -92,6 +102,6 @@ export function requireAuth(
92102
* Redirects user after logging out.
93103
*/
94104
export async function signOutAndRedirectToHome() {
95-
await auth.signOut()
96105
Router.push("/")
106+
await auth.signOut()
97107
}

pages/login.tsx

Lines changed: 2 additions & 2 deletions
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",
@@ -12,4 +12,4 @@ export async function getStaticProps({ locale }: any) {
1212
...(await serverSideTranslations(locale, ["auth", "common"]))
1313
}
1414
}
15-
}
15+
}

pages/newsfeed.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { createPage } from "../components/page"
22
import Newsfeed from "components/Newsfeed/Newsfeed"
3+
import { requireAuth } from "../components/auth"
34

45
export default createPage({
56
title: "Newsfeed",
6-
Page: () => {
7+
Page: requireAuth(() => {
78
return <Newsfeed />
8-
}
9+
})
910
})
1011

1112
// this must only be on pages in the pages folder

public/locales/en/auth.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"subscribeNewsletter": "Click here to subscribe to our newsletter",
5555
"verifyEmail": "Verify your email address",
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.",
57-
"setUpProfile": "Set Up Your Profile"
58-
57+
"setUpProfile": "Set Up Your Profile",
58+
"signInToAccess": "Login required to access this the content"
5959

6060
}

0 commit comments

Comments
 (0)