I am having trouble understanding the authentication workflow

I am trying to implement cloudsaving for my game that has been finished for quite some time now and i am held back by some confusion of the authentication workflow…

I am using

await PlayerAccountService.Instance.StartSignInAsync();

and it works!

I have a debug.log line that displays that i am signed in.

Debug.Log("Player signed in:" + PlayerAccountService.Instance.IsSignedIn);

It returns true.

I then store the token in playerprefs.

On second sign in (closing app and reopening it), i grab the token, and use

await AuthenticationService.Instance.SignInWithUnityAsync(accessToken);

the PlayerAccountService.Instance.IsSignedIn returns false…but cloudsaving still works :confused:

Please help me understand what i am doing wrong - i am so ready to release my first game!!!
Also, i apologize for the weird formatting of this post - i really tried to format it right for clarity, but this is my first time using this forum.
Thanks in advance!!!

here is the whole script:

using UnityEngine;

using Unity.Services.Core;
using Unity.Services.Authentication;
using Unity.Services.Authentication.PlayerAccounts;

public class auth_try_4 : MonoBehaviour
{
    private const string ACCESS_TOKEN = "AUTH_Token";

    private async void Start()
    {
        await UnityServices.InitializeAsync();
        StartSignIn();
        PlayerAccountService.Instance.SignedIn += OnSignedIn;
    }

    private async void StartSignIn()
    {
        if (PlayerPrefs.HasKey(ACCESS_TOKEN))
        {
            try
            {
                var accessToken = PlayerPrefs.GetString(ACCESS_TOKEN, "");
                await AuthenticationService.Instance.SignInWithUnityAsync(accessToken);

                Debug.Log("Player signed in:" + PlayerAccountService.Instance.IsSignedIn);
            }
            catch (AuthenticationException ex)
            {
                Debug.LogException(ex);
            }
            catch (RequestFailedException ex)
            {
                Debug.LogException(ex);
            }
        } else
        {
            try
            {
                await PlayerAccountService.Instance.StartSignInAsync();
               
            }
            catch (AuthenticationException ex)
            {
                Debug.LogException(ex);
            }
            catch (RequestFailedException ex)
            {
                Debug.LogException(ex);
            }
        }
    }

    private void OnSignedIn()
    {
        Debug.Log("Player Account Access token " + PlayerAccountService.Instance.AccessToken);
        AuthenticationService.Instance.SignInWithUnityAsync(PlayerAccountService.Instance.AccessToken);
        Debug.Log("Player signed in:" + PlayerAccountService.Instance.IsSignedIn);

        var accessToken = PlayerAccountService.Instance.AccessToken;
        PlayerPrefs.SetString(ACCESS_TOKEN, accessToken);
    }
}

I can’t recall that you need to manually store the access token. This session token is stored automatically by the Authentication service. So once you signed into an ID provider service successfully, the next time you can simply call SignInAnonymouslyAsync() which, if the session token exists, should sign in the player with the linked account.

At least that’s as far as I recall, I’ve only used the username/password provider so far though.

The function:
await AuthenticationService.Instance.SignInWithUnityAsync(accessToken);
requires the access token, so - i wish i knew what you meant!

I suppose if you only used username and password, it is possible that you would not need an access token, as i have not tried that yet. However, if i can not get someone on planet earth to help me understand this process, then username and password is starting to look good!
Thanks for your reply!

Once you’ve got the user signed in with the above method, you no longer need to call it!

If AuthenticationService.Instance.SessionTokenExists is true (*) then you only need to call AuthenticationService.Instance.SignInAnonymouslyAsync() in order to sign in the player with its previously linked Unity Player account.

*=property name may not be accurate, writing this from the top of my head

Check out the full tutorial here to understand session tokens and what not: Full Player Account Authentication Tutorial