Next.js Discord

Discord Forum

Configure no_page

Standard Chinchilla posted this in #help-forum
Open in Discord
Standard ChinchillaOP
Is there a way to make make a page "invisible" unless the user is logged in?

For example make so that if you try to access a control panel without being locked in it will say the page doesn't exist, but as soon as the user logs in it's there

1 Reply

Plott Hound
what you are describing is protecting routes, you can do this in middleware, in a server component or in a client component. for a server component you simply check if the session exists and return whatever you want eg:

  if (!session) {

or eg
import { notFound } from 'next/navigation'
  if (!session) {