Facebook. Can you give me the code example how correctly manage users CloudSave account?

There is the code I load data after await AuthenticationService.Instance.SignInWithFacebookAsync(aToken.TokenString);
But data is not loaded from account linked with current facebook account.

   async void Awake()
   {
       await UnityServices.InitializeAsync();
       await AuthenticationService.Instance.SignInAnonymouslyAsync();
      // InitializePlayGamesLogin();

       if (!FB.IsInitialized)
       {
           // Initialize the Facebook SDK
           FB.Init(InitCallback, OnHideUnity);
       }
       else
       {
           // Already initialized, signal an app activation App Event
           FB.ActivateApp();
           //FacebookLogin();
           GetProfilePicture();
           // AuthenticationService.Instance.
           var aToken = AccessToken.CurrentAccessToken;
           Debug.LogError(aToken);
           FacebookLogin();
       }
   }
   #region Facebook
   private void InitCallback()
   {
       if (FB.IsInitialized)
       {
           // Signal an app activation App Event
           FB.ActivateApp();
           //FacebookLogin();
           // Continue with Facebook SDK
           // ...
           if (FB.IsLoggedIn)
               GetProfilePicture();
       }
       else
       {
           Debug.Log("Failed to Initialize the Facebook SDK");
       }
   }

   private void OnHideUnity(bool isGameShown)
   {
       Time.timeScale = isGameShown ? 1 : 0;
   }

   public void FacebookLogin()
   {
       var perms = new List<string>() { "public_profile", "gaming_profile", "gaming_user_picture" };
       FB.LogInWithReadPermissions(perms, AuthCallback);
      
   }

   private async void AuthCallback(ILoginResult result)
   {
       if (FB.IsLoggedIn)
       {
           var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
           Debug.Log(aToken.UserId);
           GetProfilePicture();
           await AuthenticationService.Instance.SignInWithFacebookAsync(aToken.TokenString);
           GameLogic.Instanse.LoadData();
           //AuthenticationService.Instance.LinkWithFacebookAsync();
       }
       else
       {
           Debug.Log("User cancelled login");
       }
   }
async Task LinkWithFacebookAsync(string accessToken)
{
     try
     {
         await AuthenticationService.Instance.LinkWithFacebookAsync(accessToken);
         Debug.Log("Link is successful.");
     }
     catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
     {
         // Prompt the player with an error message.
         Debug.LogError("This user is already linked with another account. Log in instead.");
     }
     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);
     }
}

I need the script represent this logic (allow load player data(linked to facebook) when login from other device), please share if you have one!


https://docs.unity.com/ugs/en-us/manual/authentication/manual/best-practices

My data isn’t loaded because I used anonymous login before facebook login. Now I need to figure when to login anonymous and when to login to facebook and how to manage these correctly.

Looks like it is working fine now, there is code:

async void Awake()
{
    await UnityServices.InitializeAsync();
    //await AuthenticationService.Instance.SignInAnonymouslyAsync();
    if (!FB.IsInitialized)
    {
        // Initialize the Facebook SDK
    }
    else
    {
        // Already initialized, signal an app activation App Event
        FB.ActivateApp();
        GetProfilePicture();
        FacebookLogin();
    }
}

#region Facebook
private void InitCallback()
{
    if (FB.IsInitialized)
    {
        FB.ActivateApp();
        FacebookLogin();
        if (FB.IsLoggedIn)
            GetProfilePicture();
    }
    else
    {
        Debug.Log("Failed to Initialize the Facebook SDK");
    }
}

public void InitFB()
{
    FB.Init(InitCallback, OnHideUnity);
}

private async void LoginFacebookServices()
{
    var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
    try
    {
        await AuthenticationService.Instance.SignInWithFacebookAsync(aToken.TokenString);
    }
    catch (Exception ex)
    {
        Debug.LogException(ex);
    }

    GameLogic.Instanse.LoadData();
}

private void OnHideUnity(bool isGameShown)
{
    Time.timeScale = isGameShown ? 1 : 0;
}

public void FacebookLogin()
{
    var perms = new List<string>() { "public_profile", "gaming_profile", "gaming_user_picture" };
    FB.LogInWithReadPermissions(perms, AuthCallback);
}

private void AuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        GetProfilePicture();
        LoginFacebookServices();
    }
    else
    {
        Debug.Log("User cancelled login");
    }
}


void GetProfilePicture()
{
    if (FB.IsLoggedIn)
    {
        FB.API("/me/picture?type=large", HttpMethod.GET, ProfilePictureCallback);
    }
}

void ProfilePictureCallback(IGraphResult result)
{
    if (result.Error == null)
    {
        _playerAvatar.texture = result.Texture;
        _playerAvatar.GetComponent<Button>().interactable = false;
    }
    else
    {
        Debug.Log(result.Error);
    }
}