DirectoryFast
  • 🚀Get Started
  • 🔤Tutorials
    • Bring me to life!
    • How it's structured?
    • Make it yours
    • Bonus
  • 🛠️Features
    • AI
      • AI URL Scanner & Scan Jobs
    • Analytics
    • Authentication
    • API
      • Protected Endpoints
    • Blog
    • Database
    • Emails
    • Error Pages
    • Icons
    • Payments
    • Private Pages
    • SEO
  • 📦General Components
    • Shadcn/ui Components
    • Navbar
    • Footer
    • SignIn Modal
    • Hero Section
    • Social Proof
    • Collections Social Proof
    • Featured Section
    • Latest Collections Section
    • Latest Products Section
    • Recommended Section
  • 📦Directory Components
    • Product Card
    • Collection Card
    • Product Note
    • Search Bar
    • Tags
    • Product Options Toggle
    • Combobox
    • Multi-Combobox
    • Submit Product
    • Feature Product
    • Manage Note
    • Manage Collection
    • Dashboard Tables
  • ⛓️Links
    • GitHub Repository
    • Support
Powered by GitBook
On this page
  • Pages
  • API
  1. Features

Private Pages

Pages like Dashboard need authentication checks, learn how to protect them.

PreviousPaymentsNextSEO

Last updated 11 months ago

Pages

To protect your app pages, simply take advantage of the :

import { useSession } from "next-auth/react";

export default function ExamplePage() {
  const session = useSession();
  
  if (session.status === "unauthenticated") router.push("/");
  
return (
    <section>
      {session.status === "loading" && <div>Please wait</div>}
      {session.status === "unauthenticated" && <div>Please log in</div>}
      {session.status === "authenticated" && <div>Hello World</div>}
    </section>
  )
}

API

You should always protect your backend calls to avoid malicious behavior, it's similar to pages thanks to NextAuth:

import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]/options";

export async function POST(request: Request) {
  const session = await getServerSession(authOptions);
  if (session?.user.role !== "admin") {
    return new Response("Unauthorized", {
      status: 401,
      statusText: "Unauthorized",
    });
  }
  ...
}

🛠️
NextAuth Session Hook