Skip to content

Commit 3b07d27

Browse files
committed
Adding auth provider
1 parent ced307f commit 3b07d27

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

components/Newsfeed/Newsfeed.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ 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"
1920

2021
export default function Newsfeed() {
2122
const { t } = useTranslation("common")
2223

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

192193
return (
193194
<>
194-
{loading ? (
195+
{authLoading || loading ? (
195196
<Row>
196197
<Spinner animation="border" className="mx-auto" />
197198
</Row>

components/auth/AuthRedirect.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useEffect } from 'react';
2+
import { useRouter } from 'next/router';
3+
import { useAuth } from '../auth'; // Adjust this path to your actual useAuth hook
4+
5+
export function useAuthRedirect(isAuthRequired: boolean = true) {
6+
const { user, loading } = useAuth();
7+
const router = useRouter();
8+
9+
useEffect(() => {
10+
if (!loading) { // Wait until authentication state is loaded
11+
if (isAuthRequired && !user) {
12+
const redirectPath = router.asPath;
13+
// console.log("Redirecting to login:", redirectPath); // For debugging
14+
router.push(`/login?redirect=${encodeURIComponent(redirectPath)}`);
15+
}
16+
}
17+
}, [user, loading, router, isAuthRequired]); // Dependencies for useEffect
18+
19+
return { user, loading }; // Return these for use in the component if needed
20+
}

components/auth/Provider.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useEffect } from "react"
2+
import { getAuth, onAuthStateChanged, User } from "firebase/auth"
3+
import { useDispatch } from "react-redux"
4+
import { authChanged } from "./redux"
5+
6+
export const Provider: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => {
7+
const dispatch = useDispatch()
8+
9+
useEffect(() => {
10+
const auth = getAuth()
11+
const unsubscribe = onAuthStateChanged(auth, (user: User | null) => {
12+
dispatch(authChanged({ user }))
13+
})
14+
15+
return () => unsubscribe()
16+
}, [])
17+
18+
return <>{children}</>
19+
}

components/auth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +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"

components/auth/redux.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ export interface State {
2323
/** True iff user is signed in */
2424
authenticated: boolean
2525
authFlowStep: AuthFlowStep
26+
loading: boolean
2627
}
2728

2829
const initialState: State = {
2930
authenticated: false,
3031
user: undefined,
31-
authFlowStep: null
32+
authFlowStep: null,
33+
loading: true
3234
}
3335

3436
export const {
@@ -45,6 +47,7 @@ export const {
4547
state.user = payload.user
4648
state.claims = payload.claims
4749
state.authenticated = Boolean(payload.user)
50+
state.loading = false
4851
},
4952
authStepChanged(state, action: PayloadAction<AuthFlowStep>) {
5053
state.authFlowStep = action.payload

0 commit comments

Comments
 (0)