1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 15:35:22 +02:00

wait for session to load

This commit is contained in:
Karan Handa 2024-01-19 17:09:50 +05:30
parent 992e7d8513
commit f2a5eb1495

View file

@ -1,4 +1,4 @@
import { useEffect, type PropsWithChildren, type ReactElement } from 'react' import { useEffect, useState, type PropsWithChildren, type ReactElement } from 'react'
import type { AppProps } from 'next/app' import type { AppProps } from 'next/app'
import { ErrorBoundary } from 'react-error-boundary' import { ErrorBoundary } from 'react-error-boundary'
import { Analytics } from '@vercel/analytics/react' import { Analytics } from '@vercel/analytics/react'
@ -33,14 +33,18 @@ Sentry.init({
// Providers and components only relevant to a logged-in user // Providers and components only relevant to a logged-in user
const WithAuth = function ({ children }: PropsWithChildren) { const WithAuth = function ({ children }: PropsWithChildren) {
const { data: session } = useSession() const { data: session, status } = useSession()
const [isLoading, setLoading] = useState(true)
const router = useRouter() const router = useRouter()
useEffect(() => { useEffect(() => {
if (status === 'loading') return // Wait until the session status is not 'loading'
setLoading(false)
if (!session) { if (!session) {
router.push('/login') router.push('/login')
} }
}, [session, router]) }, [session, status, router])
if (session) { if (session) {
return ( return (