Web Game Account Linking

How to initiate account linking when the web game starts with an Account Link Code.

Web Game. Before or the first thing in the loading screen for the web version of your game.

1) Wait for Pley.SDK.OnInitialized to be invoked.

2) Before authentication using Pley, the game checks if Pley.ConnectionKit.GetAccountLinkCode() returns a non-empty string. If it exists, pause the loading and send the link_code to the game backend to verify it.

3) The game backend verifies the link code with Pley using connection-kit/verify-link-code.
This endpoint returns the Pley game user id and the payload that was provided to connection-kit/get-link-code.

{
    "game_user_id": "xxxxx-xxxx-xxxx-xxxxxxxxxx", // id of the pley user
    "is_guest_user": true/false,
    "payload": "user id from the game itself" // payload when code was generated
}

4) The game backend compares the two different game user id:s (Pley user and mobile user), and maps the two users together. The backend should map the user with the least progress to the user with the most progress. Read the account linking guidelines here.

5) When the game client gets a response from the game backend it invokes Pley.ConnectionKit.SetAccountLinkResult with either Ok or Failed. The backend should also set the linking status to true if successful using connection-kit/set-accountlink-status.

{
    "session_token": "session-token from pley-sdk",
    "linked": true
}

6) The game backend responds to the game client that account linking has been completed.

7) The game client calls ConnectionKit.SetAccountLinkResultAsync so Pley can notify the end user that the linking was successful.

C# code example for handling this process in the web game.

// Wait for this callback to be invoked before starting the first loading screen
SDK.OnInitialized += result =>
{
    if (result.IsError())
    {
        Debug.LogError($"Failed to initialize Pley: {result.ToString()}");
        return;
    }

  	//Check if there an linkCode exists
    var linkCodeResult = ConnectionKit.GetAccountLinkCode(out var linkCode);
    if (linkCodeResult.IsError())
    {
        Debug.LogError($"Failed to get account link code: {linkCodeResult.ToString()}");
    }

    if (string.IsNullOrEmpty(linkCode) || backendManager.HasSentLinkVerification)
    {
        // Continue with the loading screen
        return;
    }
  
  
		//Call the game backend to verify the Account Link Code.
		//If verification is successful, attach the Pley User ID to the game user in the backend.
		//Finally, return a response to the game which then calls ConnectionKit's result methods.
    backendManager.PleyConnectVerifyLinkCode(async (linkCode, error) =>
    {
        if (!string.IsNullOrEmpty(error))
        {
            await ConnectionKit.SetAccountLinkResultAsync(PleyAccountLinkResult.Failed);
            Debug.LogError($"Failed to verify Pley link code: {error}");
            return;
        }
        await ConnectionKit.SetAccountLinkResultAsync(PleyAccountLinkResult.Ok);
        // Continue the loading screen
    });
};

📘

Best practice | Before Authentication

ConnectionKit account liking is intended to occur before any backend authentication. It is best implemented before any authentication with the game backend is done.

If this is not possible, reach out to us for our alternative solution. In such a case, the game must refresh it's own IFrame to restart the game (authenticating as the newly account linked user).