This is the useEffect
code in the React component.
import { useSession } from '@supabase/auth-helpers-react'
const session = useSession()
useEffect(() => {
if (session) {
console.debug('stay in page')
} else {
console.debug('goto login')
router.push('/login').then(
() => {
console.debug('goto login done')
}
)
}
}, [session, router])
It uses a supabase to manage auth (session).
It has a very weid issue when refresh page, (login, logout redirect through router has no issues at all, only when Refresh page.), both branches will be reached, so in js console, we could see logs from both condition branches.
'stay in page'
and goto login
/ goto login done
.
I assume it is due to
session
changed after refresh. 1st time is undefined, to trigger the 2nd brachrouter.push
, immediately, when session be found, triggers the 1st branchstay in page
.
Is there any suggestion to bypass it? Or any good practice to handle page refresh in React/NextJS? Or anyone has similar or the same issue before?
session
is being fetched? – Sillabubimport { useSession, useSupabaseClient } from '@supabase/auth-helpers-react'
– Lager