2024-06-24 17:50:27 +00:00
|
|
|
import { useSearchParams } from '@solidjs/router'
|
|
|
|
import { JSX, Show, createEffect, createMemo, on } from 'solid-js'
|
|
|
|
import { useUI } from '~/context/ui'
|
2023-09-21 11:38:22 +00:00
|
|
|
import { useSession } from '../../context/session'
|
|
|
|
|
|
|
|
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) => {
|
2024-06-24 17:50:27 +00:00
|
|
|
const { session } = useSession()
|
|
|
|
const author = createMemo<number>(() => session()?.user?.app_data?.profile?.id || 0)
|
|
|
|
const [, changeSearchParams] = useSearchParams()
|
|
|
|
const { hideModal } = useUI()
|
2023-09-24 14:55:46 +00:00
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
createEffect(
|
|
|
|
on(
|
|
|
|
[() => props.disabled, author],
|
|
|
|
([disabled, a]) => {
|
|
|
|
if (disabled || !a) return
|
|
|
|
if (a) {
|
|
|
|
console.debug('[AuthGuard] profile is loaded')
|
|
|
|
hideModal()
|
|
|
|
} else {
|
|
|
|
changeSearchParams(
|
|
|
|
{
|
|
|
|
source: 'authguard',
|
2024-06-26 08:22:05 +00:00
|
|
|
m: 'auth'
|
2024-06-24 17:50:27 +00:00
|
|
|
},
|
2024-06-26 08:22:05 +00:00
|
|
|
{ replace: true }
|
2024-06-24 17:50:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2024-06-26 08:22:05 +00:00
|
|
|
{ defer: true }
|
|
|
|
)
|
2024-06-24 17:50:27 +00:00
|
|
|
)
|
2023-09-21 11:38:22 +00:00
|
|
|
|
2024-06-24 17:50:27 +00:00
|
|
|
return <Show when={author() || props.disabled}>{props.children}</Show>
|
2023-09-21 11:38:22 +00:00
|
|
|
}
|