Add support for public and private routes for access control based on authentication #81854
phoenixfusionx
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
public
orprivate
without writing repetitive logic.Non-Goals
No response
Background
Currently, handling public and private routes in Next.js requires boilerplate in each route using
getServerSession
,useSession
, or custom middleware. This approach quickly becomes repetitive and error-prone, especially in large applications with multiple protected routes.Some developers build their own abstractions, but there is no official or standardized way to declare route-level access rules. A declarative configuration or convention (e.g., metadata on route files or a centralized config) would improve DX and reduce redundancy. Other frameworks (like Remix or Nuxt) offer similar patterns and have demonstrated value in streamlining auth flows.
Proposal
Introduce a built-in mechanism to label routes (in the app directory) as public or private. This could be achieved through:
conifg
export inpage.tsx
orlayout.tsx
, e.g:+private.tsx
, though this may be less scalable.When
auth: 'private'
is set, Next.js can:getServerSession
.redirectTo: '/auth/login'
).This would reduce complexity for developers and encourage consistent patterns for access control.
Beta Was this translation helpful? Give feedback.
All reactions