import Link from 'next/link' import { useTranslation } from 'next-i18next' import { useAccount } from 'shared/hooks/use-account.mjs' import { useBackend } from 'shared/hooks/use-backend.mjs' import { roles } from 'config/roles.mjs' import { useEffect, useState } from 'react' import { Loading } from 'shared/components/spinner.mjs' export const ns = ['auth'] const Wrap = ({ children }) => (
{children}
) const ContactSupport = ({ t }) => (
{t('contactSupport')}
) const AuthRequired = ({ t, banner }) => ( {banner}

{t('authRequired')}

{t('membersOnly')}

{t('signUp')} {t('signIn')}
) const AccountInactive = ({ t, banner }) => ( {banner}

{t('accountInactive')}

{t('accountInactiveMsg')}

{t('signupAgain')}

{t('signUp')}
) const AccountDisabled = ({ t, banner }) => ( {banner}

{t('accountDisabled')}

{t('accountDisabledMsg')}

) const AccountProhibited = ({ t, banner }) => ( {banner}

{t('accountProhibited')}

{t('accountProhibitedMsg')}

) const AccountStatusUnknown = ({ t, banner }) => ( {banner}

{t('statusUnknown')}

{t('statusUnknownMsg')}

) const RoleLacking = ({ t, requiredRole, role, banner }) => ( {banner}

{t('roleLacking')}

) const ConsentLacking = ({ t, banner }) => ( {banner}

{t('consentLacking')}

{t('membersOnly')}

{t('signUp')} {t('signIn')}
) export const AuthWrapper = ({ children, app, requiredRole = 'user' }) => { const { t } = useTranslation(ns) const { account, token, admin, stopImpersonating } = useAccount() const backend = useBackend() const [ready, setReady] = useState(false) const [impersonating, setImpersonating] = useState(false) /* * Avoid hydration errors */ useEffect(() => { const verifyAdmin = async () => { const result = await backend.adminPing(admin.token) if (result.success && result.data.account.role === 'admin') { setImpersonating({ admin: result.data.account.username, user: account.username, }) } } if (admin && admin.token) verifyAdmin() setReady(true) }, [admin]) if (!ready) return const banner = impersonating ? (
Hi {impersonating.admin}, you are currently impersonating {impersonating.user}
) : null const childProps = { t, banner } if (!token || !account.username) return if (account.status !== 1) { if (account.status === 0) return if (account.status === -1) return if (account.status === -2) return return } if (account.consent < 1) return if (!roles.levels[account.role] || roles.levels[account.role] < roles.levels[requiredRole]) { return } return children }