webapp/src/components/AuthGuard/AuthGuard.tsx

41 lines
1.0 KiB
TypeScript
Raw Normal View History

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'
import { useSession } from '../../context/session'
type Props = {
children: JSX.Element
disabled?: boolean
}
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()
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
)
2024-06-24 17:50:27 +00:00
return <Show when={author() || props.disabled}>{props.children}</Show>
}