Files
PenguinsTwoChase/Assets/Scripts/Network/Authenticator.cs

47 lines
1.4 KiB
C#

using PashaBibko.PenguinChase.Extensions;
using Unity.Services.Authentication;
using Unity.Services.Core;
using System.Collections;
using UnityEngine;
namespace PashaBibko.PenguinChase.Network
{
public static class Authenticator
{
public static bool IsAuthenticated { get; private set; }
private static bool sIsAuthenticating;
public static IEnumerator Authenticate()
{
// Early return if already authenticated
Debug.Log($"Authenticate called, [Authenticated: {IsAuthenticated}]");
if (IsAuthenticated)
{
yield break;
}
// Stops multiple authentication attempts at the same time
if (sIsAuthenticating)
{
yield return new WaitUntil(() => IsAuthenticated || !sIsAuthenticating);
yield break; // User should be logged in from other attempt
}
sIsAuthenticating = true;
Debug.Log("Initializing Unity Services");
yield return UnityServices
.InitializeAsync()
.Await();
Debug.Log("Signing in anonymously");
yield return AuthenticationService.Instance
.SignInAnonymouslyAsync()
.Await();
Debug.Log("User has been authenticated");
sIsAuthenticating = false;
IsAuthenticated = true;
}
}
}