Mirror Networking
NetworkAuthenticator.cs
1using System;
2using UnityEngine;
3using UnityEngine.Events;
4
5namespace Mirror
6{
7 [Serializable] public class UnityEventNetworkConnection : UnityEvent<NetworkConnectionToClient> {}
8
10 [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-authenticators")]
11 public abstract class NetworkAuthenticator : MonoBehaviour
12 {
14 [Header("Event Listeners (optional)")]
15 [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
17
19 [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
20 public UnityEvent OnClientAuthenticated = new UnityEvent();
21
23 public virtual void OnStartServer() {}
24
26 public virtual void OnStopServer() {}
27
30
31 protected void ServerAccept(NetworkConnectionToClient conn)
32 {
33 OnServerAuthenticated.Invoke(conn);
34 }
35
36 protected void ServerReject(NetworkConnectionToClient conn)
37 {
38 conn.Disconnect();
39 }
40
42 public virtual void OnStartClient() {}
43
45 public virtual void OnStopClient() {}
46
48 public virtual void OnClientAuthenticate() {}
49
50 protected void ClientAccept()
51 {
52 OnClientAuthenticated.Invoke();
53 }
54
55 protected void ClientReject()
56 {
57 // Set this on the client for local reference
58 NetworkClient.connection.isAuthenticated = false;
59
60 // disconnect the client
61 NetworkClient.connection.Disconnect();
62 }
63
64 // Reset() instead of OnValidate():
65 // Any NetworkAuthenticator assigns itself to the NetworkManager, this is fine on first adding it,
66 // but if someone intentionally sets Authenticator to null on the NetworkManager again then the
67 // Authenticator will reassign itself if a value in the inspector is changed.
68 // My change switches OnValidate to Reset since Reset is only called when the component is first
69 // added (or reset is pressed).
70 void Reset()
71 {
72#if UNITY_EDITOR
73 // automatically assign authenticator field if we add this to NetworkManager
74 NetworkManager manager = GetComponent<NetworkManager>();
75 if (manager != null && manager.authenticator == null)
76 {
77 // undo has to be called before the change happens
78 UnityEditor.Undo.RecordObject(manager, "Assigned NetworkManager authenticator");
79 manager.authenticator = this;
80 }
81#endif
82 }
83 }
84}
Base class for implementing component-based authentication during the Connect phase
virtual void OnClientAuthenticate()
Called on client from OnClientConnectInternal when a client needs to authenticate
UnityEvent OnClientAuthenticated
Notify subscribers on the client when the client is authenticated
virtual void OnServerAuthenticate(NetworkConnectionToClient conn)
Called on server from OnServerConnectInternal when a client needs to authenticate
virtual void OnStartClient()
Called when client starts, used to register message handlers if needed.
virtual void OnStopClient()
Called when client stops, used to unregister message handlers if needed.
UnityEventNetworkConnection OnServerAuthenticated
Notify subscribers on the server when a client is authenticated
virtual void OnStopServer()
Called when server stops, used to unregister message handlers if needed.
virtual void OnStartServer()
Called when server starts, used to register message handlers if needed.