TalkBox uses JSON Web Tokens (JWT) for stateless authentication. When a user registers or logs in, the server issues a signed token that the client stores locally and attaches to every subsequent request — both REST calls and Socket.IO connections. ADocumentation Index
Fetch the complete documentation index at: https://mintlify.com/abdelhafid37/talkbox/llms.txt
Use this file to discover all available pages before exploring further.
<ProtectedRoute> component on the client side ensures that only authenticated users can access the chat interface.
Registration
New accounts are created by posting user credentials to the registration endpoint. The server validates all fields, checks for duplicates, hashes the password with bcrypt, and persists the new user to MongoDB. Endpoint:POST /api/auth/register
Request body:
201 Created:
Validation rules enforced by the server:
usernamemust be a string between 3 and 30 characters (whitespace is trimmed before checking).emailmust match the pattern/^[a-zA-Z0-9\.]{2,}@[a-zA-Z]{2,}\.[a-zA-Z]{2,}$/.passwordmust be at least 8 characters long.- A duplicate
usernameoremailreturns409 Conflictwith a message identifying which field is already taken.
Password hashing
Passwords are never stored in plain text. The server hashes them with bcrypt at 10 salt rounds before writing to the database:Login
Existing users authenticate with their email and password. On success, the server returns a signed JWT valid for 7 days. Endpoint:POST /api/auth/login
Request body:
200 OK:
JWT_SECRET from the server environment. Its payload contains a single claim:
Token storage and usage (client)
Persist the token
After a successful login the client stores the raw JWT string in On page reload,
localStorage under the key "token":AuthProvider initialises its token state directly from localStorage:Attach the token to REST requests
Every protected REST call includes the token as an
Authorization: Bearer header. The message service is a representative example:Protected routes (client)
The<ProtectedRoute> component wraps any route that requires authentication. It reads the current token from AuthContext and redirects unauthenticated visitors to /login:
/chat route is wrapped with this component in the router configuration, so users who are not logged in are automatically sent to the login page.
JWT middleware (server)
Every protected API route is guarded byauthMiddleware. It reads the Authorization header, verifies the token against JWT_SECRET, and attaches the decoded payload to req.user for downstream controllers:
req.user.userId to identify the acting user — for example, messageController uses it to set the sender field when creating a new message.
Socket.IO connections go through an equivalent middleware that reads socket.handshake.auth.token and attaches the full User document to socket.user for the duration of the session.
Making an authenticated request
The example below shows how to fetch a conversation history using the JWT token retrieved fromlocalStorage:
Logout
Logging out clears the token from both React state andlocalStorage, and disconnects the socket:
token becomes null, the AuthProvider effect calls socket.disconnect() and the <ProtectedRoute> guard redirects the user back to /login on the next render.