Retrieve all anonymous messages that have been sent to the currently authenticated user. The route uses a four-stage MongoDB aggregation pipeline to unwind, sort, and regroup the embeddedDocumentation 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.
messages array so results always arrive in newest-first order. If the user has no messages the endpoint returns an empty array — it never returns 404 for an empty inbox.
Method and URL
Authentication
Required. The request must carry a valid NextAuth JWT session cookie. The server callsgetServerSession(NEXT_AUTH_CONFIG) to verify the caller’s identity. Unauthenticated requests receive a 401 response.
Request Parameters
This endpoint accepts no query parameters and no request body.How It Works — The Aggregation Pipeline
Because messages are stored as an embedded array inside theUser document, a plain findById cannot sort the subdocuments efficiently. The route uses a four-stage pipeline:
.aggregate() is always an array; the actual messages live at userMessages[0].messages.
String ID → ObjectId conversion is mandatory inside aggregation pipelines. NextAuth stores
_id as a plain string in the session object. Mongoose auto-converts strings to ObjectId in methods like findById, but it does not do so inside aggregate. Without new mongoose.Types.ObjectId(user._id), the $match stage finds zero documents and the response is always an empty array — even when messages exist.curl Example
Responses
200 — Success
Returned whether the user has messages or not. An empty inbox returnsmessages: [], not a 404.
401 — Not Authenticated
No valid session cookie was present.500 — Internal Server Error
An unexpected error occurred during the aggregation.Response Fields
true on a successful fetch (including an empty inbox); false on any error.Array of message objects belonging to the authenticated user, sorted newest-first. Present only on a 200 response.
MongoDB
ObjectId of the individual message, serialised as a hex string. Use this value as messageId when calling PATCH /api/delete-message.The text body of the anonymous message.
ISO 8601 timestamp set by the server at the moment the message was received (e.g.
"2024-05-24T10:30:58.000Z").