Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use client'

import { useRouter } from 'next/navigation'
import { useEffect, useRef } from 'react'
import { toast } from 'sonner'

export function ContestAccessDenied() {
const router = useRouter()
const done = useRef(false)

useEffect(() => {
if (done.current) {
return
}
done.current = true
router.replace('/admin/contest')
setTimeout(() => {
toast.error('You do not have access to this contest.')
}, 100)
}, [router])

return null
}
41 changes: 41 additions & 0 deletions apps/frontend/app/admin/contest/[contestId]/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { safeFetcherWithAuth } from '@/libs/utils'
import { ContestRole, type UserContest } from '@generated/graphql'
import { ContestAccessDenied } from './_components/ContestAccessDenied'

export const dynamic = 'force-dynamic'

async function fetchContestRoles() {
try {
const response: UserContest[] = await safeFetcherWithAuth
.get('contest/role')
.json()

return response
} catch (error) {
console.error('Error fetching contest roles:', error)
return []
}
}

export default async function Layout({
children,
params
}: {
children: React.ReactNode
params: Promise<{ contestId: string }>
}) {
const { contestId } = await params
const roles = await fetchContestRoles()

const hasPermission = roles.some(
(r) =>
r.contestId === Number(contestId) &&
(r.role === ContestRole.Admin || r.role === ContestRole.Manager)
)

if (!hasPermission) {
return <ContestAccessDenied />
}

return children
}
Loading