When your Agents are deployed, to keep things secure, send a token from the client, then verify it on the server. This mirrors the shape used in PartyKit’s auth guide.
WebSocket authentication
WebSockets are not HTTP, so the handshake is limited when making cross-domain connections.
What you cannot send
- Custom headers during the upgrade
Authorization: Bearer ... on connect
What works
- Put a signed, short-lived token in the connection URL as query parameters
- Verify the token in your server’s connect path
Never place raw secrets in URLs. Prefer a JWT or a signed token that expires quickly and is scoped to the user or room.
Same origin
If the client and server share the origin, the browser will send cookies during the WebSocket handshake. Session based auth can work here. Prefer HTTP-only cookies.
Cross origin
Cookies do not help across origins. Pass credentials in the URL query, then verify on the server.
Usage examples
Static authentication
Async authentication
Build query values right before connect. Use Suspense for async setup.
JWT refresh pattern
Refresh the token when the connection fails due to authentication error.
Cross-domain authentication
Pass credentials in the URL when connecting to another host, then verify on the server.