Sign in a cached player is not working. [annonymous, already signed in.]

Hello,
I made a game that requires player to come back to the first scene every time side scenes are done. I put my authentication on the first scene. Also, I used anonymous sign in because personally I experienced more fun without logging in to specific platforms with this kind of game.

I followed this link to sign my cached player because I thought it can help me to not 'double login' : https://docs.unity.com/ugs/manual/authentication/manual/cached-players
the script:

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);
     } 
}

and this is my code:

public async void Start()
    {
        UnityServices.InitializeAsync();
        await SignInCachedUserAsync();
    }


    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!");

            DailyRewardButton.interactable = true;
            LoadData();

            // 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);
        }
}

and this error occurs whenever the player comes back to the first scene:

AuthenticationException: Invalid state for this operation. The player is already signed in.
AuthenticationManager.SignInCachedUserAsync () (at Assets/Scripts/AuthenticationManager.cs:42)
UnityEngine.Debug:LogException(Exception)
<SignInCachedUserAsync>d__6:MoveNext() (at Assets/Scripts/AuthenticationManager.cs:55)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<SignInCachedUserAsync>d__6&)
AuthenticationManager:SignInCachedUserAsync()
<Start>d__5:MoveNext() (at Assets/Scripts/AuthenticationManager.cs:24)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<Start>d__5&)
AuthenticationManager:Start()

Are there anything I can do to fix? :( thank you so much for advance :sweat_smile:

Hi

You need to change your early validation, having a cached player doesn't mean you are signed in.

Instead of checking if (!AuthenticationService.Instance.SessionTokenExists) return;

You can instead check if (AuthenticationService.Instance.IsSignedIn) return;

When you return to your original scene, it will see you are already signed in and skip sign in.

Hello @erickb_unity , thank you so much with the super fast reply, i really appreciate it :smile:

unfortunately, I've tried the solution you offered but to no avail :(
i can make it working now because i used another scene only for the one call authentication and never access it again.