Skip to content

Commit

Permalink
Add docs for using builtin auth mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Wiborg committed Feb 26, 2025
1 parent 6361c66 commit bfa5410
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tutorials/networking/high_level_multiplayer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,45 @@ a dedicated server with no GPU available. See
server. You'll have to modify them so the server isn't considered to be a
player. You'll also have to modify the game starting mechanism so that the
first player who joins can start the game.

Authentication
--------------

Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access.
For this you can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism.

On the server:

.. tabs::
.. code-tab:: gdscript GDScript

# after multiplayer.multiplayer_peer = peer
multiplayer.auth_timout = 3
multiplayer.auth_callback = func(peer_id: int, payload: PackedByteArray):
var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8())
# your authentication logic goes here...

# signal to the MultiplayerAPI that the authentication was successful
if authentication_successful:
multiplayer.complete_auth(peer_id)

On the client:

.. tabs::
.. code-tab:: gdscript GDScript

# after multiplayer.multiplayer_peer = peer
multiplayer.auth_callback = func:
# we have to set this on the client in order for the peer_authenticating signal to fire
pass
multiplayer.peer_authenticating.connect(func(peer_id: int):
var auth_data = {
"username": "username",
"password": "password",
}
multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer())

# signal to the MultiplayerAPI that the authentication was successful
multiplayer.complete_auth(peer_id)

As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, thet connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire.

0 comments on commit bfa5410

Please sign in to comment.