pummmp.fun/components/onboarding-guard.tsx
2026-07-04 12:49:09 -07:00

36 lines
1 KiB
TypeScript

'use client'
import { useSession } from 'next-auth/react'
import { useRouter, usePathname } from 'next/navigation'
import { useEffect } from 'react'
export function OnboardingGuard({ children }: { children: React.ReactNode }) {
const { data: session, status } = useSession()
const router = useRouter()
const pathname = usePathname()
useEffect(() => {
if (status === 'loading') return
if (session?.user?.isOnboarding) {
if (pathname !== '/onboarding') {
router.push('/onboarding')
}
} else {
if (pathname === '/onboarding') {
router.push('/')
}
}
}, [session, status, pathname, router])
//! while checking, we might want to return null to block content rendering,
//! but that might cause flicker
//! for now, we render children, but the redirect will happen fast
//! if we really want to block, we can render nothing if the check fails
if (status === 'authenticated' && session?.user?.isOnboarding && pathname !== '/onboarding') {
return null;
}
return <>{children}</>
}