Next.js Discord

Discord Forum

Configure no_page

Unanswered
Standard Chinchilla posted this in #help-forum
Open in Discord
Avatar
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

Avatar
@Standard Chinchilla 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
Avatar
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) {
    redirect("/api/auth/signin");
  }

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