Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Authentication' started by unity_lKj2L45mNspniw, Nov 16, 2023.

  1. unity_lKj2L45mNspniw

    unity_lKj2L45mNspniw

    Joined:
    Dec 20, 2019
    Posts:
    8
    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:

    Code (CSharp):
    1. using Unity.Services.Authentication;
    2.  
    3. async Task SignInCachedUserAsync()
    4. {
    5.     // Check if a cached player already exists by checking if the session token exists
    6.     if (!AuthenticationService.Instance.SessionTokenExists)
    7.     {
    8.         // if not, then do nothing
    9.         return;
    10.     }
    11.  
    12.     // Sign in Anonymously
    13.     // This call will sign in the cached player.
    14.     try
    15.     {
    16.         await AuthenticationService.Instance.SignInAnonymouslyAsync();
    17.         Debug.Log("Sign in anonymously succeeded!");
    18.  
    19.         // Shows how to get the playerID
    20.         Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");  
    21.     }
    22.     catch (AuthenticationException ex)
    23.     {
    24.         // Compare error code to AuthenticationErrorCodes
    25.         // Notify the player with the proper error message
    26.         Debug.LogException(ex);
    27.     }
    28.     catch (RequestFailedException ex)
    29.     {
    30.         // Compare error code to CommonErrorCodes
    31.         // Notify the player with the proper error message
    32.         Debug.LogException(ex);
    33.      }
    34. }
    and this is my code:


    Code (CSharp):
    1. public async void Start()
    2.     {
    3.         UnityServices.InitializeAsync();
    4.         await SignInCachedUserAsync();
    5.     }
    6.  
    7.  
    8.     async Task SignInCachedUserAsync()
    9.     {
    10.         // Check if a cached player already exists by checking if the session token exists
    11.         if (!AuthenticationService.Instance.SessionTokenExists)
    12.         {
    13.             // if not, then do nothing
    14.             return;
    15.         }
    16.  
    17.         // Sign in Anonymously
    18.         // This call will sign in the cached player.
    19.         try
    20.         {
    21.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    22.             Debug.Log("Sign in anonymously succeeded!");
    23.  
    24.             DailyRewardButton.interactable = true;
    25.             LoadData();
    26.  
    27.             // Shows how to get the playerID
    28.             Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
    29.         }
    30.         catch (AuthenticationException ex)
    31.         {
    32.             // Compare error code to AuthenticationErrorCodes
    33.             // Notify the player with the proper error message
    34.             Debug.LogException(ex);
    35.         }
    36.         catch (RequestFailedException ex)
    37.         {
    38.             // Compare error code to CommonErrorCodes
    39.             // Notify the player with the proper error message
    40.             Debug.LogException(ex);
    41.         }
    42. }
    and this error occurs whenever the player comes back to the first scene:

    Code (Boo):
    1. AuthenticationException: Invalid state for this operation. The player is already signed in.
    2. AuthenticationManager.SignInCachedUserAsync () (at Assets/Scripts/AuthenticationManager.cs:42)
    3. UnityEngine.Debug:LogException(Exception)
    4. <SignInCachedUserAsync>d__6:MoveNext() (at Assets/Scripts/AuthenticationManager.cs:55)
    5. System.Runtime.CompilerServices.AsyncTaskMethodBuilder:Start(<SignInCachedUserAsync>d__6&)
    6. AuthenticationManager:SignInCachedUserAsync()
    7. <Start>d__5:MoveNext() (at Assets/Scripts/AuthenticationManager.cs:24)
    8. System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start(<Start>d__5&)
    9. AuthenticationManager:Start()
    Are there anything I can do to fix? :( thank you so much for advance :oops:
     
  2. erickb_unity

    erickb_unity

    Unity Technologies

    Joined:
    Sep 1, 2021
    Posts:
    90
    Hi

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

    Instead of checking
    Code (CSharp):
    1. if (!AuthenticationService.Instance.SessionTokenExists) return;
    You can instead check
    Code (CSharp):
    1. if (AuthenticationService.Instance.IsSignedIn) return;
    When you return to your original scene, it will see you are already signed in and skip sign in.
     
  3. unity_lKj2L45mNspniw

    unity_lKj2L45mNspniw

    Joined:
    Dec 20, 2019
    Posts:
    8
    Hello @erickb_unity , thank you so much with the super fast reply, i really appreciate it :D

    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.