2021-05-22 23:41:18 +02:00
|
|
|
import React, {useEffect} from 'react'
|
|
|
|
|
import {useRouter} from 'next/router'
|
2020-12-04 20:16:24 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-12-12 03:26:40 +01:00
|
|
|
import {createMuiTheme, ThemeProvider} from '@material-ui/core/styles'
|
2020-12-04 20:16:24 +01:00
|
|
|
import CssBaseline from '@material-ui/core/CssBaseline'
|
2021-05-22 23:41:18 +02:00
|
|
|
import {Provider, useSession} from 'next-auth/client'
|
2020-12-12 03:26:40 +01:00
|
|
|
|
|
|
|
|
const darkTheme = createMuiTheme({
|
|
|
|
|
palette: {
|
|
|
|
|
type: 'dark',
|
|
|
|
|
primary: {
|
|
|
|
|
light: '#81c784',
|
|
|
|
|
main: '#4caf50',
|
|
|
|
|
dark: '#388e3c',
|
|
|
|
|
contrastText: '#fff'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-12-04 20:16:24 +01:00
|
|
|
|
|
|
|
|
export default function MyApp(props) {
|
|
|
|
|
const {Component, pageProps} = props
|
|
|
|
|
|
2021-05-22 23:41:18 +02:00
|
|
|
useEffect(() => {
|
2020-12-04 20:16:24 +01:00
|
|
|
const jssStyles = document.querySelector('#jss-server-side')
|
|
|
|
|
if (jssStyles) {
|
|
|
|
|
jssStyles.remove()
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
return (
|
2020-12-24 13:37:22 +01:00
|
|
|
<ThemeProvider theme={darkTheme}>
|
|
|
|
|
<CssBaseline />
|
2021-05-22 23:41:18 +02:00
|
|
|
<Provider session={pageProps.session}>
|
|
|
|
|
{Component.auth ? (
|
|
|
|
|
<Auth><Component {...pageProps} /></Auth>
|
|
|
|
|
) : (
|
|
|
|
|
<Component {...pageProps} />
|
|
|
|
|
)}
|
|
|
|
|
</Provider>
|
2020-12-24 13:37:22 +01:00
|
|
|
</ThemeProvider>
|
2020-12-04 20:16:24 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-22 23:41:18 +02:00
|
|
|
function Auth({children}) {
|
|
|
|
|
const [session, loading] = useSession()
|
|
|
|
|
const isUser = Boolean(session?.user)
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (loading) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isUser) {
|
|
|
|
|
router.push('/kont')
|
|
|
|
|
}
|
|
|
|
|
}, [isUser, loading, router])
|
|
|
|
|
|
|
|
|
|
if (isUser) {
|
|
|
|
|
return children
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div>Loading...</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-04 20:16:24 +01:00
|
|
|
MyApp.propTypes = {
|
|
|
|
|
Component: PropTypes.elementType.isRequired,
|
|
|
|
|
pageProps: PropTypes.object.isRequired
|
|
|
|
|
}
|
2021-05-22 23:41:18 +02:00
|
|
|
|
|
|
|
|
Auth.propTypes = {
|
|
|
|
|
children: PropTypes.node.isRequired
|
|
|
|
|
}
|