Chat App uses JSON Web Tokens (JWT) for stateless authentication. When a user signs up or logs in, the server generates a signed JWT and delivers it as an HTTP-only cookie — never exposed to JavaScript — so the client is automatically authenticated on every subsequent request without any manual token management.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/khushboodaryani/Chat-App/llms.txt
Use this file to discover all available pages before exploring further.
How Signup Works
A new account is created by posting five fields toPOST /api/auth/signup. The server validates the input, hashes the password, assigns an auto-generated avatar, and sets the JWT cookie before responding.
Required fields
The user’s display name shown throughout the UI.
Must be unique across all accounts. The server returns
400 if the username is already taken.Minimum 6 characters. Hashed with bcryptjs using 10 salt rounds before storage.
Must match
password exactly — validated on both the client (via useSignup) and the server.Either
"male" or "female". Determines which avatar URL is auto-assigned.201 Created
The password is never returned in any response. The server selects only
_id, fullName, username, and profilePic.How Login Works
POST /api/auth/login accepts a username and password, verifies the credentials, and issues a fresh JWT cookie on success.
- The server calls
User.findOne({ username })to locate the account. bcrypt.compare(password, user.password)validates the supplied password against the stored hash.- If either check fails the server responds
400with{ "error": "Invalid username or password" }— a deliberately generic message that avoids leaking whether the username exists. - On success,
generateTokenAndSetCookiesigns a new JWT and attaches it to the response as thejwtcookie.
200 OK
JWT Cookie Configuration
The token is set by thegenerateTokenAndSetCookie utility on every successful signup or login:
Security properties at a glance
| Flag | Value | Purpose |
|---|---|---|
httpOnly | true | JavaScript cannot read the cookie, preventing XSS token theft |
sameSite | "strict" | Cookie is never attached to cross-origin requests, preventing CSRF |
secure | true in production | Cookie is only transmitted over HTTPS when NODE_ENV !== "development" |
maxAge | 15 days | Token expires automatically — no explicit refresh flow needed |
Route Protection
All messaging and user endpoints are gated by theprotectRoute middleware:
req.user is populated with the full user document minus the password hash. Every downstream controller can safely read req.user._id, req.user.username, and so on without an additional database call.
Logout
POST /api/auth/logout immediately invalidates the session by overwriting the cookie with an empty value and a maxAge of 0, which instructs the browser to delete it:
useLogout hook also removes the cached user object from localStorage and resets authUser to null in the AuthContext, so the app redirects to the login screen.
Frontend Authentication Hooks
All three auth operations are encapsulated in custom React hooks that manage theloading state and surface errors via react-hot-toast.
| Hook | Signature | Returns | Side effects |
|---|---|---|---|
useSignup | signup({ fullName, username, password, confirmPassword, gender }) | { loading, signup } | Sets authUser in context; writes to localStorage |
useLogin | login(username, password) | { loading, login } | Sets authUser in context; writes to localStorage |
useLogout | logout() | { loading, logout } | Clears authUser; removes chat-user from localStorage |
useLogin in a component