2
0
Files
bot/apps/builder/components/HOC/withUser.tsx

21 lines
557 B
TypeScript
Raw Normal View History

2021-11-29 15:19:07 +01:00
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/router'
import { useEffect } from 'react'
const withAuth =
2021-12-06 15:48:50 +01:00
(WrappedComponent: (props: any) => JSX.Element) =>
(props: JSX.IntrinsicAttributes) => {
2021-11-29 15:19:07 +01:00
const router = useRouter()
2021-12-06 15:48:50 +01:00
const { status } = useSession()
2021-11-29 15:19:07 +01:00
useEffect(() => {
if (!router.isReady) return
if (status === 'loading') return
if (status === 'unauthenticated') router.replace('/signin')
}, [status, router])
2021-12-06 15:48:50 +01:00
return <WrappedComponent {...props} />
2021-11-29 15:19:07 +01:00
}
export default withAuth