Learn how to link Google accounts to Stash
Before you configure account linking, you need to:
- Download and import the Google Play Games Plugin for Unity.
- Enable Google sign-in for your app. Follow Google’s documentation on how to configure your game for Google Login.
- 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. Stash then verifies that Google issued the token, and that the player's session is valid.
void InitializePlayGamesLogin() {
// Important to include RequestIdToken(), this will fetch a valid id token.
var config = new PlayGamesClientConfiguration.Builder().RequestIdToken().Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
}
void LoginGoogle() {
Social.localUser.Authenticate(OnGoogleLogin);
}
void OnGoogleLogin(bool success) {
if (success) {
// Call Unity Authentication SDK to sign in or link with Google.
// Get the IdToken using "GetIdToken" function.
var idToken = ((PlayGamesLocalUser) Social.localUser)
.GetIdToken() Debug.Log($"Login successful - {idToken}");
} else {
Debug.Log("Unsuccessful login");
}
}
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][Google Account] 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 5 months ago