mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-04 23:17:12 +01:00
move to next-auth
This commit is contained in:
committed by
Thomas Ruoff
parent
9f4180388b
commit
b257bc8258
@@ -1,14 +1,40 @@
|
||||
import '../styles/index.css'
|
||||
import UserContext from '../context/user'
|
||||
import { useEffect } from 'react';
|
||||
|
||||
export default function MyApp({ Component, pageProps }) {
|
||||
const { username, role } = pageProps?.user || {}
|
||||
import { useSession, signIn, SessionProvider } from "next-auth/react"
|
||||
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="wrapper">
|
||||
<UserContext.Provider value={{ username, role }}>
|
||||
<Component {...pageProps} />
|
||||
</UserContext.Provider>
|
||||
<SessionProvider session={session}>
|
||||
{Component.auth ? (
|
||||
<Auth>
|
||||
<Component {...pageProps} />
|
||||
</Auth>
|
||||
) : (
|
||||
<Component {...pageProps} />
|
||||
)}
|
||||
</SessionProvider>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user