Skip to content

Commit

Permalink
fix(next): ensures admin access only blocks admin routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed May 2, 2024
1 parent 2b5c1ba commit 3290376
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/next/src/utilities/initPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const initPage = async ({
const globalSlug = entityType === 'globals' ? entitySlug : undefined
const docID = collectionSlug && createOrID !== 'create' ? createOrID : undefined

const isAdminRoute = route.startsWith(adminRoute)
const isAuthRoute = authRoutes.some((r) => r === route.replace(adminRoute, ''))

if (redirectUnauthenticatedUser && !user && !isAuthRoute) {
Expand All @@ -111,7 +112,7 @@ export const initPage = async ({
redirect(`${routes.admin}/login?redirect=${route + stringifiedSearchParams}`)
}

if (!permissions.canAccessAdmin && !isAuthRoute) {
if (!permissions.canAccessAdmin && isAdminRoute && !isAuthRoute) {
notFound()
}

Expand Down
7 changes: 5 additions & 2 deletions packages/next/src/views/NotFound/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ export const NotFoundPage = async ({
[key: string]: string | string[]
}
}) => {
const config = await configPromise
const { routes: { admin: adminRoute } = {} } = config

const initPageResult = await initPage({
config: configPromise,
config,
redirectUnauthenticatedUser: true,
route: '/not-found',
route: `${adminRoute}/not-found`,
searchParams,
})

Expand Down

0 comments on commit 3290376

Please sign in to comment.