Authentication
Use the server engineauth() hook to authenticate a player before RPGJS lets the
WebSocket connection join a room.
In MMORPG mode, RPGJS connects to one room at a time. The first connection
usually targets lobby-*, then map changes reconnect the player to map-*
rooms. The auth() hook is called for each RPGJS room connection. If it throws,
the connection is refused before a RpgPlayer is created or restored in that
room.
Mental model
Useauth() as the global RPGJS gate:
- it runs on the lobby connection;
- it runs again when the player reconnects to a map room;
- it accepts or refuses the room connection;
- it returns the stable public player id used by RPGJS and Signe user collections.
Server
Addauth() to the server engine hooks. Return the stable public player id for
the authenticated account.
publicId used by @users(RpgPlayer).
Return the same id for the same account on every room connection so the player
keeps the same identity when moving between maps.
If auth() returns undefined, RPGJS keeps the default behavior and lets the
room system generate the public player id.
Use the authenticated id for save slots
Authentication and save loading are separate operations:auth()verifies the credential and returns the account’s stable public id.- RPGJS exposes that id as
player.idto server-side game code and storage strategies. - A
SaveStorageStrategyusesplayer.idto list, save, or retrieve that account’s slots. - The game explicitly calls
player.load(slot)after it has selected a slot.
player.load(). It only gives
the server a stable, trusted key with which to find the correct saves.
sessionId as the long-term account key. It is
useful for reconnecting a browser, but it can change when the player clears local
storage, changes device, or starts a new browser session.
Similarly, do not accept a playerId sent in a save/load request from the
browser. Use the authenticated player.id already attached to the server-side
RpgPlayer instance.
What happens on reconnect
- A map change authenticates the connection again and transfers the live player state to the destination room. Do not reload a save slot on every map join.
- A browser refresh may restore the current private session and room state, but this is not a long-term save load.
- A new session can authenticate as the same account. Your login or title-screen flow must then list or load the desired slot explicitly.
0 in the server-side onConnected hook
is a simple policy. For multiple characters, list the slots after authentication
and let the player select one. See When an MMORPG save is loaded
for both approaches.
Public lobby, protected maps
If your lobby must stay public, returnundefined for lobby rooms and only
enforce authentication on map rooms:
auth() returns a valid stable player id.
If the public lobby displays account save slots, authenticate the account before
listing them. A generated anonymous lobby id cannot identify the account’s
long-term saves.
Client
In a browser, send credentials through the connection query. RPGJS sends this query on the initial connection and again when the player reconnects to another room.onConnectError hook:
onConnectError is the place to recover.
The client onConnected hook runs only after the server accepts the RPGJS
connection.
By default, the MMORPG client does not retry a connection that closes before
RPGJS accepts it. This avoids retry loops when a token is invalid. If you need
custom retry behavior, pass PartySocket options through socketOptions:
Guards
auth() is global RPGJS authentication. It decides whether the player may enter
the game and which public id represents the player.
Signe room guards are still useful for more specific authorization rules:
- protect a custom room;
- protect one action;
- protect an HTTP request handler;
- check roles or permissions after authentication.
auth() to identify the player, then use a Signe @Guard() to
restrict an admin action.