pos-dashboard-v2/src/hocs/AuthGuard.tsx
2025-08-05 21:31:53 +07:00

25 lines
700 B
TypeScript

'use client'
// Type Imports
import type { Locale } from '@configs/i18n'
import type { ChildrenType } from '@core/types'
import { useEffect } from 'react'
import { useAuth } from '../contexts/authContext'
// Component Imports
import { redirect } from 'next/navigation'
import Loading from '../components/layout/shared/Loading'
import { getLocalizedUrl } from '../utils/i18n'
export default function AuthGuard({ children, locale }: ChildrenType & { locale: Locale }) {
const { isAuthenticated } = useAuth()
useEffect(() => {
if (!isAuthenticated) {
redirect(getLocalizedUrl('/login', locale))
}
}, [isAuthenticated])
return <>{isAuthenticated ? children : <Loading />}</>
}