Skip to content

Commit 85e0915

Browse files
authored
Merge pull request #10719 from baccenfutter/high-level-multiplayer-authentication
Add docs for using builtin auth mechanism
2 parents a563dcd + 4b86411 commit 85e0915

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

tutorials/networking/high_level_multiplayer.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,48 @@ a dedicated server with no GPU available. See
672672
server. You'll have to modify them so the server isn't considered to be a
673673
player. You'll also have to modify the game starting mechanism so that the
674674
first player who joins can start the game.
675+
676+
Authentication
677+
--------------
678+
679+
Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access.
680+
You can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s built-in authentication mechanism for this.
681+
682+
On the server:
683+
684+
.. tabs::
685+
.. code-tab:: gdscript GDScript
686+
687+
# This goes after `multiplayer.multiplayer_peer = peer`.
688+
multiplayer.auth_timout = 3
689+
multiplayer.auth_callback = func(peer_id: int, payload: PackedByteArray):
690+
var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8())
691+
# Your authentication logic (such as checking the supplied username/password against a database)
692+
693+
# Tell the MultiplayerAPI that the authentication was successful
694+
if authentication_successful:
695+
multiplayer.complete_auth(peer_id)
696+
697+
On the client:
698+
699+
.. tabs::
700+
.. code-tab:: gdscript GDScript
701+
702+
# This goes after `multiplayer.multiplayer_peer = peer`.
703+
multiplayer.auth_callback = func:
704+
# We have to set this on the client for the `peer_authenticating`
705+
# signal to emit.
706+
pass
707+
multiplayer.peer_authenticating.connect(func(peer_id: int):
708+
var auth_data = {
709+
"username": "username",
710+
"password": "password",
711+
}
712+
multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer())
713+
714+
# Tell the MultiplayerAPI that the authentication was successful.
715+
multiplayer.complete_auth(peer_id)
716+
717+
As soon as both, the client's and the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`
718+
methods have been called, the connection is considered to be established and the
719+
`connected_to_server` and `peer_connected` signals fire.

0 commit comments

Comments
 (0)