The dashboard is the control centre for every AnonMessage user. It shows all received anonymous messages in reverse-chronological order, lets users pause and resume incoming messages, provides a one-click link-copy widget, and allows individual message deletion — all behind a protected route that requires a valid NextAuth JWT session.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dev0302/nextjs-project-1/llms.txt
Use this file to discover all available pages before exploring further.
Access and Route Protection
The dashboard lives at/dashboard. The Next.js middleware checks for a valid JWT on every request to this path:
/sign-in. Once authenticated, the Dashboard Server Component calls getServerSession to read the current user’s identity:
session.user._id to fetch the live isAcceptingMessages status from MongoDB, and session.user.username is used by the CopyLink client component to build the shareable URL.
Typical User Workflow
Land on the dashboard
After signing in, the user is redirected to
/dashboard. The Server
Component fetches the current isAcceptingMessages flag from the database
and passes it as a prop to MessageToggle.Check the message inbox
The
MessageCard Server Component is wrapped in <Suspense> with an
animated skeleton fallback. While it fetches messages, three pulsing
placeholder cards are shown. Once ready, all received messages are displayed
in a responsive grid sorted newest-first.Toggle message acceptance
The
MessageToggle switch lets the user pause or resume incoming messages
instantly. Flipping it calls POST /api/accept-messages and shows a toast
confirmation. If the API call fails, the switch reverts to its previous
state.Share the profile link
The
CopyLink widget displays the user’s full public URL
(/u/[username]) and provides a Copy button. After copying, the button
briefly shows a green ✓ checkmark for two seconds.Message List — Aggregation Pipeline
Messages are fetched byGET /api/get-messages. Because messages are embedded subdocuments inside the User document, a plain findById cannot sort the internal array. The route uses a MongoDB aggregation pipeline to unwind, sort, and regroup the messages:
| Stage | Purpose |
|---|---|
$match | Selects the single user document by _id |
$unwind | Flattens the messages array so each message becomes its own document |
$sort | Sorts individual messages by createdAt descending (newest first) |
$group | Reassembles the sorted messages back into a single array |
{ success: true, messages: [] } rather than a 404.
The dashboard uses React
<Suspense> with a MessagesSkeleton fallback while
MessageCard (a Server Component) fetches data. The skeleton renders three
animated placeholder cards (animate-pulse) so the layout does not shift when
messages load. Because MessageCard fetches with cache: "no-store", it
always reflects the latest data on each page visit.Message Acceptance Toggle
MessageToggle is a "use client" component that receives the current isAcceptingMessages value as the initialIsAccepting prop (read directly from MongoDB in the Server Component):
POST /api/accept-messages validates the boolean payload against a Zod schema, then runs:
Shareable Link — CopyLink Component
CopyLink is a "use client" component at src/components/CopyLink.tsx that reads session.user.username from the NextAuth client session to build the profile URL:
NEXT_PUBLIC_BASE_URL must be set in the environment (e.g. https://yourdomain.com) for the copied URL to be correct in production.
Deleting Messages
Each message card triggers aPATCH request with the message ID as a query parameter:
$pull operator to remove the matching subdocument from the user’s messages array in a single atomic update:
MessageGrid performs an optimistic removal — the card disappears immediately with a CSS transition while the API call completes in the background.
Session Data Used
The dashboard relies on two fields fromsession.user:
jwt callback in lib/auth.ts and are always available in any Server Component via getServerSession(NEXT_AUTH_CONFIG).