pos-dashboard-v2/src/hocs/GuestOnlyRoute.tsx

35 lines
774 B
TypeScript
Raw Normal View History

2025-08-14 00:29:19 +07:00
'use client'
2025-08-05 12:35:40 +07:00
// Next Imports
// Type Imports
import type { ChildrenType } from '@core/types'
2025-08-14 00:29:19 +07:00
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
import { useAuth } from '../contexts/authContext'
import Loading from '../components/layout/shared/Loading'
2025-08-05 12:35:40 +07:00
// Config Imports
// Util Imports
2025-08-14 00:29:19 +07:00
const GuestOnlyRoute = ({ children }: ChildrenType) => {
const router = useRouter()
const { isAuthenticated, currentUser } = useAuth()
2025-08-05 12:35:40 +07:00
2025-08-14 00:29:19 +07:00
useEffect(() => {
if (!isAuthenticated) return
2025-08-05 12:35:40 +07:00
2025-08-14 00:29:19 +07:00
if (currentUser?.role === 'admin') {
router.push('/dashboards/overview')
} else {
router.push('/sa/organizations/list')
}
}, [isAuthenticated])
2025-08-05 12:35:40 +07:00
2025-08-14 00:29:19 +07:00
return <>{isAuthenticated ? <Loading /> : children}</>
2025-08-05 12:35:40 +07:00
}
export default GuestOnlyRoute