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 ![]()
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);
}
}