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'
import { horFlexClasses } from 'shared/utils.mjs'
import { LockIcon, PlusIcon } from 'shared/components/icons.mjs'
import { ConsentForm, ns as gdprNs } from 'shared/components/gdpr/form.mjs'
export const ns = ['auth', 'gdpr']
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 = ({ banner, refresh }) => {
const { setAccount, setToken, setSeenUser } = useAccount()
const backend = useBackend()
const { t } = useTranslation(ns)
const updateConsent = async ({ consent1, consent2 }) => {
let consent = 0
if (consent1) consent = 1
if (consent1 && consent2) consent = 2
if (consent > 0) {
const result = await backend.updateConsent(consent)
console.log({ result })
if (result.success) {
setToken(result.data.token)
setAccount(result.data.account)
setSeenUser(result.data.account.username)
refresh()
} else {
console.log('something went wrong', result)
refresh()
}
}
}
return (
{banner}
)
}
export const AuthWrapper = ({ children, requiredRole = 'user' }) => {
const { t } = useTranslation(ns)
const { account, token, admin, stopImpersonating, signOut } = useAccount()
const backend = useBackend()
const [ready, setReady] = useState(false)
const [impersonating, setImpersonating] = useState(false)
const [error, setError] = useState(false)
const [refreshCount, setRefreshCount] = useState(0)
/*
* 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,
})
}
setReady(true)
}
const verifyUser = async () => {
const result = await backend.ping()
if (!result.success) {
if (result.data?.error?.error) setError(result.data.error.error)
else signOut()
}
setReady(true)
}
if (admin && admin.token) verifyAdmin()
if (token) verifyUser()
else setReady(true)
}, [admin, token, refreshCount])
const refresh = () => {
setRefreshCount(refreshCount + 1)
setError(false)
}
if (!ready)
return (
<>
not ready
>
)
const banner = impersonating ? (
Hi {impersonating.admin}, you are currently impersonating {impersonating.user}
) : null
const childProps = { t, banner }
if (!token || !account.username) return
if (error) {
if (error === 'accountInactive') return
if (error === 'accountDisabled') return
if (error === 'accountBlocked') return
if (error === 'consentLacking') return
return
}
if (!roles.levels[account.role] || roles.levels[account.role] < roles.levels[requiredRole]) {
return
}
return children
}