# Private Pages

### Pages

To protect your app pages, simply take advantage of the [NextAuth Session Hook](https://next-auth.js.org/getting-started/client#usesession):

{% code lineNumbers="true" %}

```typescript
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>
  )
}
```

{% endcode %}

### API

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

{% code lineNumbers="true" %}

```typescript
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",
    });
  }
  ...
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.directoryfa.st/features/private-pages.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
