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'
|
2023-09-29 12:48:58 +00:00
|
|
|
import { useRouter } from '../../stores/router'
|
|
|
|
import { RootSearchParams } from '../../pages/types'
|
|
|
|
import { AuthModalSearchParams } from '../Nav/AuthModal/types'
|
2023-09-21 11:38:22 +00:00
|
|
|
|
|
|
|
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-29 12:48:58 +00:00
|
|
|
const { changeSearchParam } = useRouter<RootSearchParams & AuthModalSearchParams>()
|
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 {
|
2023-09-29 12:48:58 +00:00
|
|
|
changeSearchParam(
|
|
|
|
{
|
|
|
|
source: 'authguard',
|
|
|
|
modal: 'auth'
|
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
2023-09-21 11:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|