Skip to content

Commit bfa5410

Browse files
author
Brian Wiborg
committed
Add docs for using builtin auth mechanism
1 parent 6361c66 commit bfa5410

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tutorials/networking/high_level_multiplayer.rst

+42
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,45 @@ a dedicated server with no GPU available. See
670670
server. You'll have to modify them so the server isn't considered to be a
671671
player. You'll also have to modify the game starting mechanism so that the
672672
first player who joins can start the game.
673+
674+
Authentication
675+
--------------
676+
677+
Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access.
678+
For this you can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism.
679+
680+
On the server:
681+
682+
.. tabs::
683+
.. code-tab:: gdscript GDScript
684+
685+
# after multiplayer.multiplayer_peer = peer
686+
multiplayer.auth_timout = 3
687+
multiplayer.auth_callback = func(peer_id: int, payload: PackedByteArray):
688+
var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8())
689+
# your authentication logic goes here...
690+
691+
# signal to the MultiplayerAPI that the authentication was successful
692+
if authentication_successful:
693+
multiplayer.complete_auth(peer_id)
694+
695+
On the client:
696+
697+
.. tabs::
698+
.. code-tab:: gdscript GDScript
699+
700+
# after multiplayer.multiplayer_peer = peer
701+
multiplayer.auth_callback = func:
702+
# we have to set this on the client in order for the peer_authenticating signal to fire
703+
pass
704+
multiplayer.peer_authenticating.connect(func(peer_id: int):
705+
var auth_data = {
706+
"username": "username",
707+
"password": "password",
708+
}
709+
multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer())
710+
711+
# signal to the MultiplayerAPI that the authentication was successful
712+
multiplayer.complete_auth(peer_id)
713+
714+
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 commit comments

Comments
 (0)