Apple ID
Learn how to link Apple ID accounts to Stash
Before you configure account linking, you need to:
- Install the Apple Game Kit Unity plugin. The GameKit framework is used to incorporate Apple Game Center features, including player identities.
- Set up your App Store Connect app and ensure you have the Game Center capability set up.
- Download and import the Stash Plugin for Unity.
1. Retrieve the idToken
idToken
The idToken
is required for authenticating users and linking their accounts. This token adheres to the JWT standard, and is passed to Stash’s servers.
using System.Text;
using UnityEngine;
// External dependencies
using AppleAuth;
using AppleAuth.Enums;
using AppleAuth.Interfaces;
using AppleAuth.Native;
public class AppleExampleScript : MonoBehaviour
{
IAppleAuthManager m_AppleAuthManager;
public string Token { get; private set; }
public string Error { get; private set; }
public void Initialize()
{
var deserializer = new PayloadDeserializer();
m_AppleAuthManager = new AppleAuthManager(deserializer);
}
public void Update()
{
if (m_AppleAuthManager != null)
{
m_AppleAuthManager.Update();
}
}
public void LoginToApple()
{
// Initialize the Apple Auth Manager
if (m_AppleAuthManager == null)
{
Initialize();
}
// Set the login arguments
var loginArgs = new AppleAuthLoginArgs(LoginOptions.IncludeEmail | LoginOptions.IncludeFullName);
// Perform the login
m_AppleAuthManager.LoginWithAppleId(
loginArgs,
credential =>
{
var appleIDCredential = credential as IAppleIDCredential;
if (appleIDCredential != null)
{
var idToken = Encoding.UTF8.GetString(
appleIDCredential.IdentityToken,
0,
appleIDCredential.IdentityToken.Length);
Debug.Log("Sign-in with Apple successfully done. IDToken: " + idToken);
Token = idToken;
}
else
{
Debug.Log("Sign-in with Apple error. Message: appleIDCredential is null");
Error = "Retrieving Apple Id Token failed.";
}
},
error =>
{
Debug.Log("Sign-in with Apple error. Message: " + error);
Error = "Retrieving Apple Id Token failed.";
}
);
}
}
2. Link accounts
You use Stash's LinkAccount
method to link accounts. This method uses the idToken
from the last step, as well as:
- The internal user ID that's used for purchase identification.
- The code challenge associated with the linking request.
Stash recommends refreshing credentials before linking to Stash. Credentials older than 60 minutes are considered expired, which causes the linking process to fail.
//Wrap in try-catch block and handle StashRequestError appropriately.
try
{
LinkResponse response = await StashClient.LinkAccount(_stashChallenge, InternalPlayerId, idToken);
//Linking successful, prompt player to return back to the web shop.
}
catch (StashRequestError e)
{
Debug.LogError($"[STASH][Apple] Account linking failed. Code: {e.Code}, Message: {e.Message}");
}
The accounts are now linked and users are returned to the web shop to complete the flow that triggered the linking process.
Updated 6 months ago