2023-09-21 11:38:22 +00:00
|
|
|
import { createEffect, JSX, Show } from 'solid-js'
|
|
|
|
import { useSession } from '../../context/session'
|
|
|
|
import { hideModal, showModal } from '../../stores/ui'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
children: JSX.Element
|
2023-09-24 14:55:46 +00:00
|
|
|
disabled?: boolean
|
2023-09-21 11:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const AuthGuard = (props: Props) => {
|
|
|
|
const { isAuthenticated, isSessionLoaded } = useSession()
|
2023-09-24 14:55:46 +00:00
|
|
|
|
2023-09-21 11:38:22 +00:00
|
|
|
createEffect(() => {
|
2023-09-24 14:55:46 +00:00
|
|
|
if (props.disabled) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-21 11:38:22 +00:00
|
|
|
if (isSessionLoaded()) {
|
|
|
|
if (isAuthenticated()) {
|
|
|
|
hideModal()
|
|
|
|
} else {
|
|
|
|
showModal('auth', 'authguard')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-09-24 14:55:46 +00:00
|
|
|
return <Show when={(isSessionLoaded() && isAuthenticated()) || props.disabled}>{props.children}</Show>
|
2023-09-21 11:38:22 +00:00
|
|
|
}
|