Skip to content

Commit

Permalink
feat: adding callback that can be used to handle Authentication Failed
Browse files Browse the repository at this point in the history
SetAuthenticationFailedCallback should be used to set the callback, the callback should manually call `player.Disconnect();` once it has dont handling other parts of the fail
  • Loading branch information
James-Frowen committed Apr 16, 2024
1 parent bb52865 commit 1cdbfb6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public class NetworkServer : MonoBehaviour
private SyncVarReceiver _syncVarReceiver;
public MessageHandler MessageHandler { get; private set; }

private Action<INetworkPlayer, AuthenticationResult> _authFallCallback;

/// <summary>
/// Set to true if you want to manually call <see cref="UpdateReceive"/> and <see cref="UpdateSent"/> and stop mirage from automatically calling them
/// </summary>
Expand Down Expand Up @@ -378,11 +380,27 @@ private async UniTaskVoid AuthenticateAsync(INetworkPlayer player)
}
else
{
// todo use reason
player.Disconnect();
if (_authFallCallback != null)
{
if (logger.LogEnabled()) logger.Log($"Calling user auth failed callback");
_authFallCallback.Invoke(player, result);
}
else
{
if (logger.LogEnabled()) logger.Log($"Default auth failed, disconnecting player");
player.Disconnect();
}
}
}

public void SetAuthenticationFailedCallback(Action<INetworkPlayer, AuthenticationResult> callback)
{
if (_authFallCallback != null && callback != null)
if (logger.WarnEnabled()) logger.LogWarning($"Replacing old callback. Only 1 auth failed callback can be used at once");

_authFallCallback = callback;
}

private void AuthenticationSuccess(INetworkPlayer player, AuthenticationResult result)
{
player.SetAuthentication(new PlayerAuthentication(result.Authenticator, result.Data));
Expand Down

0 comments on commit 1cdbfb6

Please sign in to comment.