This endpoint lets clients verify whether a desired username is available before submitting the full sign-up form. It validates the input against theDocumentation 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.
usernameSchema Zod rules and, if the format is valid, queries MongoDB to check for an existing document with that username.
Method and URL
Query Parameters
The username to check for availability. Must be 2–20 characters long and contain only letters (
a-z, A-Z), digits (0-9), or underscores (_).Validation Rules
The value is parsed byusernameSchema (defined in src/app/schemas/signUpSchema.ts) before any database query is made:
| Rule | Constraint | Error message |
|---|---|---|
| Minimum length | 2 characters | "Username must be atleast 2 characters" |
| Maximum length | 20 characters | "username must not be more than 20" |
| Allowed characters | /^[a-zA-Z0-9_]+$/ | "Username can only contain letters, numbers, and underscores" |
Responses
All responses share theApiResponse shape:
200 — Username available
The username passed validation and no matching document was found in MongoDB.200 — Username taken
The username passed validation but is already registered in the database.400 — Invalid format
The value failed Zod validation. Themessage field contains the first validation error.
401 — Server error
An unexpected error occurred while connecting to the database or running the query.Both the “available” and “taken” outcomes return HTTP 200. Always inspect the
success boolean in the response body — do not rely on the status code alone to determine availability.Response Fields
true if the username is available; false if it is already taken, failed validation, or an error occurred.Human-readable description of the outcome. Mirrors the Zod error text verbatim when validation fails.
Example Request
How It Works
- The handler extracts
usernamefromreq.urlvianew URL(req.url).searchParams. - The raw value is wrapped in
{ username }and passed toUsernameQuerySchema.safeParse(). - If parsing fails, the first Zod issue’s
messageis returned immediately with a400status — no database call is made. - On a valid parse,
dbConnect()establishes (or reuses) the MongoDB connection andUser.findOne({ username })is executed. - If a document is found the endpoint returns
{ success: false, message: "Username already exists" }; otherwise{ success: true, message: "Username available" }— both with status200.
Usage in the UI
The sign-up form calls this endpoint as anonChange handler while the user types their desired username, providing real-time availability feedback without waiting for full form submission.