initial commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
'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()
|
||||
|
||||
console.log('isAuthenticated', isAuthenticated)
|
||||
|
||||
useEffect(() => {
|
||||
if (!isAuthenticated) {
|
||||
redirect(getLocalizedUrl('/login', locale))
|
||||
}
|
||||
}, [isAuthenticated])
|
||||
|
||||
return <>{isAuthenticated ? children : <Loading />}</>
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// Next Imports
|
||||
|
||||
// Type Imports
|
||||
import type { Locale } from '@configs/i18n'
|
||||
import type { ChildrenType } from '@core/types'
|
||||
|
||||
// Config Imports
|
||||
|
||||
// Util Imports
|
||||
|
||||
const GuestOnlyRoute = async ({ children, lang }: ChildrenType & { lang: Locale }) => {
|
||||
// const session = await getServerSession()
|
||||
|
||||
// if (session) {
|
||||
// redirect(getLocalizedUrl(themeConfig.homePageUrl, lang))
|
||||
// }
|
||||
|
||||
console.log(lang)
|
||||
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
export default GuestOnlyRoute
|
||||
@@ -0,0 +1,28 @@
|
||||
// Next Imports
|
||||
import type { headers } from 'next/headers'
|
||||
|
||||
// Type Imports
|
||||
import type { Locale } from '@configs/i18n'
|
||||
import type { ChildrenType } from '@core/types'
|
||||
|
||||
// Component Imports
|
||||
import LangRedirect from '@components/LangRedirect'
|
||||
|
||||
// Config Imports
|
||||
import { i18n } from '@configs/i18n'
|
||||
|
||||
// ℹ️ We've to create this array because next.js makes request with `_next` prefix for static/asset files
|
||||
const invalidLangs = ['_next']
|
||||
|
||||
const TranslationWrapper = (
|
||||
props: { headersList: Awaited<ReturnType<typeof headers>>; lang: Locale } & ChildrenType
|
||||
) => {
|
||||
const doesLangExist = i18n.locales.includes(props.lang)
|
||||
|
||||
// ℹ️ This doesn't mean MISSING, it means INVALID
|
||||
const isInvalidLang = invalidLangs.includes(props.lang)
|
||||
|
||||
return doesLangExist || isInvalidLang ? props.children : <LangRedirect />
|
||||
}
|
||||
|
||||
export default TranslationWrapper
|
||||
Reference in New Issue
Block a user