Sign in a cached player

A cached player is a player who has their session token cached on the SDK. Every time a sign in event is successful, the SDK caches a session token. A cached player exists if these conditions are fulfilled:

  • The player previously signed in with Unity Authentication via anonymous login or platform login.
  • The cached session token hasn't been deleted.

To verify if a session token is currently cached for the current profile, use AuthenticationService.Instance.SessionTokenExists.

If the session token exists, then the SignInAnonymouslyAsync() method recovers the existing credentials of a player, regardless of whether they signed in anonymously or through a platform account. A code example would look like this:

using Unity.Services.Authentication;

async Task SignInCachedUserAsync()
{
    // Check if a cached player already exists by checking if the session token exists
    if (!AuthenticationService.Instance.SessionTokenExists) 
    {
        // if not, then do nothing
        return;
    }

    // Sign in Anonymously
    // This call will sign in the cached player.
    try
    {
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
        Debug.Log("Sign in anonymously succeeded!");

        // Shows how to get the playerID
        Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");   
    }
    catch (AuthenticationException ex)
    {
        // Compare error code to AuthenticationErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
        // Compare error code to CommonErrorCodes
        // Notify the player with the proper error message
        Debug.LogException(ex);
     }  
}

Note that the sample code is a method only, not a class.