Skip to content

Private Pages

Fresh

Source: shipfa.st/docs/tutorials/private-page

Once user authentication is set up, you can build private routes like dashboards, accounts, etc.

How It Works

The layout.js in /dashboard ensures all pages and subpages are private. Unauthenticated users are redirected to the login page (configured in config.js under auth).

Example: User Dashboard

javascript
import { getServerSession } from "next-auth";
import { authOptions } from "@/libs/next-auth";
import connectMongo from "@/libs/mongoose";
import User from "@/models/User";

export default async function Dashboard() {
  await connectMongo();
  const session = await getServerSession(authOptions);
  const user = await User.findById(session.user.id);

  return (
    <>
      <main className="min-h-screen p-8 pb-24">
        <section className="max-w-xl mx-auto space-y-8">
          <h1 className="text-3xl md:text-4xl font-extrabold">
            User Dashboard
          </h1>
          <p>Welcome {user.name}</p>
          <p>Your email is {user.email}</p>
        </section>
      </main>
    </>
  );
}

See Also