AnonMessage does not expose a hand-written sign-in route. Authentication is handled entirely by NextAuth.js using the built-inDocumentation 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.
CredentialsProvider. When a user submits their credentials, NextAuth processes the request at POST /api/auth/callback/credentials, validates the credentials against MongoDB, and issues a signed JWT session cookie that carries custom user fields.
How Sign-In Works
NextAuth intercepts requests to/api/auth/callback/credentials — you do not call this URL directly from application code. Instead, you use the signIn() helper from next-auth/react (client components) or inspect the session with getServerSession() (server components and API routes). Under the hood, NextAuth’s authorize callback:
- Connects to MongoDB via
dbConnect(). - Looks up the user document by email (
User.findOne({ email })). - Calls
bcrypt.compare(password, user.password)to validate the supplied password against the stored hash. - Returns the user object on success, or throws a descriptive error (
"User not found","Invalid password") on failure.
Credentials
| Field | Type | Description |
|---|---|---|
email | string | The registered email address of the account. |
password | string | The plain-text password. Compared against the bcrypt hash stored in MongoDB. |
JWT and Session Enrichment
After a successfulauthorize call, NextAuth runs the jwt and session callbacks to attach custom fields that are not part of the NextAuth default type definitions. The following fields are added to every JWT and forwarded into the session object:
| Field | Source |
|---|---|
_id | user.id?.toString() — the MongoDB ObjectId as a string |
isVerified | Whether the user completed OTP verification at sign-up |
isAcceptingMessages | Whether the user’s inbox is currently open to anonymous messages |
username | The user’s chosen display name |
Session Object Shape
The full session object available in both server and client contexts conforms to the following TypeScript interface, extended vianext-auth.d.ts:
Calling Sign-In from a Client Component
Use thesignIn function from next-auth/react. Pass 'credentials' as the provider ID along with the user’s email and password. Supply a callbackUrl to control where NextAuth redirects on success.
Reading the Session in Server Components
UsegetServerSession with the exported NEXT_AUTH_CONFIG options object to access the enriched session on the server side without an additional network request.
Reading the Session in Client Components
Use theuseSession hook from next-auth/react. Wrap the component tree with NextAuth’s SessionProvider at the layout level for this hook to work.
Error Handling
Whenauthorize throws an error, NextAuth appends an error query parameter to the redirect URL. For CredentialsProvider, the value is always CredentialsSignin regardless of the underlying message (e.g., “User not found” vs. “Invalid password”).
Sign Out
CallsignOut from next-auth/react to clear the JWT session cookie and redirect the user.
The
NEXTAUTH_SECRET environment variable must be set in your .env.local file (and in your production environment). NextAuth uses this value to sign and verify the JWT session cookie. Without it, the application will fail to start or will be unable to verify session tokens.