How it works
Authentication is handled transparently:- When you visit a room URL, the middleware checks if you have an auth token
- If not, a unique token is generated and stored in a cookie
- The token is added to the room’s participant list in Redis
- All API requests validate the token before allowing access
Token generation
Tokens are created in the middleware when you first access a room:src/proxy.ts
Tokens are generated using nanoid, the same library used for room IDs. They’re cryptographically random and collision-resistant.
Cookie configuration
Thex-auth-token cookie is configured with:
- httpOnly - Prevents JavaScript access, protecting against XSS attacks
- secure - HTTPS-only in production
- sameSite: strict - Prevents CSRF attacks
- path: / - Available across the entire site
API authentication
All API endpoints (except room creation) require authentication:src/app/api/[[...slugs]]/auth.ts
- Extracts the token from the
x-auth-tokencookie - Fetches the room’s participant list from Redis
- Verifies the token is in the list
- Returns 401 Unauthorized if validation fails
Username generation
Users don’t create accounts, so they need anonymous identities. Usernames are automatically generated using a combination of random words, animals, and a unique ID:src/hooks/use-username.ts
clever-fox-aB3d9
Usernames are:
- Generated once - On first visit, combining a random word + random animal + 5-character ID
- Stored in localStorage - Persists across browser sessions using the key
custom-username - Not validated server-side - Only the max length (100 characters) is validated
- Purely cosmetic - Used only for display in the UI to differentiate “YOU” from other participants
Access control flow
Here’s how authentication works when joining a room:Middleware checks for existing token
If the cookie exists and is valid for this room, the user is allowed in.
Middleware checks room capacity
If the room already has 2 participants, reject with “ROOM FULL” error.
Token persistence
Tokens are stored in cookies, so they:- Persist across page refreshes
- Survive browser restarts (until the room expires)
- Are automatically sent with all API requests
- Expire when the room expires (via Redis TTL)
Clearing your cookies will require you to rejoin the room (if capacity allows).
Security considerations
What this system protects against
- Unauthorized access - Only participants with valid tokens can send/receive messages
- Room overflow - The 2-participant limit is enforced server-side
- Token theft - HttpOnly cookies prevent JavaScript access
- CSRF - SameSite:strict prevents cross-origin requests
What this system does NOT protect against
- Man-in-the-middle attacks - Use HTTPS in production
- Shared computers - Cookies persist until cleared
- Room ID guessing - Room IDs are random but not secret
Authentication errors
| Error | Status | Cause | Solution |
|---|---|---|---|
| Missing Room ID or Token | 401 | Cookie missing or roomId not in query | Rejoin the room URL |
| Invalid token | 401 | Token not in room’s participant list | Clear cookies and rejoin |
| Room full | 302 | Room already has 2 participants | Wait or create a new room |
Next steps
Room management
Learn how room access control works
Architecture overview
See how authentication fits into the overall system