Custom ID sign-in

Minimum SDK version: 3.1.0

This article guides you through the following scenarios to set up authentication for players in your game with a Custom Token:

  • Set up Custom ID Provider sign-in.
  • Sign in a returning user or create a new user.

To provide a Custom ID sign-in option for the players in your game, set up your app to enable sign-in with a Custom ID provider.

Set up a Custom ID Sign In

  1. Add Custom ID as an ID provider for Unity:

    1. In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication from the navigation menu.
    2. Set ID Providers to Custom, then select Add.
  2. Create a Service Account and add project role Player Authentication Token Issuer

Sign in a returning player or create new player

  1. To sign a player in using the Custom ID Authentication you must make a request to your own game server to request a Unity Authentication Service Access Token and Session Token.

    On your game server:

    1. Call the Token Exchange API to retrieve a stateless token.
    2. Call the Sign In With Custom ID API.
  2. Use the Access Token and Session Token retrieved from your game server to call the ProcessAuthenticationTokens API.

Process Authentication Tokens

ProcessAuthenticationTokens is used to process Unity Authentication Service Access Tokens and make them available to other UGS SDKs integrated into the game that require the player to be authenticated. Since the access tokens expire you must manually refresh the access token and call ProcessAuthenticationTokens with the new access token. You can call ProcessAuthenticationTokens with both the access token and the session token. The Unity Authentication SDK refreshes the access token before it expires, keeps the player's session active, and enables sign in a cached player.

using Unity.Services.Authentication;

void SignUserWithCustomTokenWithAutoRefresh()
{
    try
    {
        // Check if a cached player already exists by checking if the session token exists
        if (AuthenticationService.Instance.SessionTokenExists) 
        {
            // This call will sign in the cached player.
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Cached user sign in succeeded!");
        }
        else
        {
            var userTokens = // Fetch the user tokens using your method calls
            AuthenticationService.Instance.ProcessAuthenticationTokens(userTokens.AccessToken, userTokens.SessionToken)
        }
        // Shows how to get the playerID
        Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");   
    }
    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);
    } 
    catch (Exception ex) {
        // Handle exceptions from your method call
        Debug.LogException(ex);
    }
}

Note that the sample code is a method only, not a class.