mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 06:57:12 +01:00
cookie's now only used under /admin
This commit is contained in:
21
components/auth.tsx
Normal file
21
components/auth.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
import { useSession, signIn } from 'next-auth/react'
|
||||
|
||||
export default function Auth({ children }) {
|
||||
const { data: session, status } = useSession()
|
||||
const isUser = !!session?.user
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'loading') return // Do nothing while loading
|
||||
if (!isUser) signIn() // If not authenticated, force log in
|
||||
}, [isUser, status])
|
||||
|
||||
if (isUser) {
|
||||
return children
|
||||
}
|
||||
|
||||
// Session is being fetched, or no user.
|
||||
// If no user, useEffect() will redirect.
|
||||
return <div>Loading...</div>
|
||||
}
|
||||
Reference in New Issue
Block a user