mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { AxiomWebVitals } from 'next-axiom'
|
|
import { Analytics } from '@vercel/analytics/react';
|
|
import { useEffect } from 'react'
|
|
|
|
import type { AppProps } from 'next/app'
|
|
|
|
import { useSession, signIn, SessionProvider } from 'next-auth/react'
|
|
import Layout from '../components/layout';
|
|
|
|
import '../styles/index.css'
|
|
|
|
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>
|
|
}
|
|
|
|
export default function MyApp({
|
|
Component,
|
|
pageProps: { session, ...pageProps },
|
|
}) {
|
|
return (
|
|
<div className="flex flex-col min-h-screen">
|
|
<SessionProvider session={session}>
|
|
<Layout>
|
|
{Component.authenticationRequired ? (
|
|
<Auth>
|
|
<Component {...pageProps} />
|
|
</Auth>
|
|
) : (
|
|
<Component {...pageProps} />
|
|
)}
|
|
<Analytics />
|
|
</Layout>
|
|
</SessionProvider>
|
|
<AxiomWebVitals />
|
|
</div>
|
|
)
|
|
} |