Code-Link

Minimum SDK version: 3.0.0

See the following scenarios in setting up authentication for players in your game with Code-Link:

  • Set up Code-Link.
  • Sign in using Code-Link.
  1. Configure your ID provider to be Code-Link for Unity Authentication:
    1. In the Unity Editor menu, go to Edit > Project Settings…, then select Services > Authentication.
    2. Set ID Providers to Code-Link, then select Add.
    3. Select Save.

Sign in as a player using these Code-Link functions:

  1. The player signs in to device A anonymously or using any of the supported ID providers.
  2. The player decides to use the same account on device B (not signed in) and asks device B to generate a Sign In Code.
  3. When they receive the Sign In Code the player inputs the code in Device A to Confirm the Code is Authorized.
  4. The player returns to device B and requests to Sign In with Code.
  5. Device B is authenticated with the same account as device A.

> Note: Step 4 can be skipped if device B polls the service until the Code is either confirmed or expired.

To sign in using Code-Link follow these steps:

  1. Use the GenerateSignInCodeAsync method (passing an identifier is optional but recommended to identify the device that generated the sign in code) from the unauthenticated device that wants to authenticate to generate a Sign In Code.

  2. Show the generated SignInCode and expiration to the player.

    Note: The identifier has a maximum length of 128 characters.

    async Task GenerateCodeAsync(string identifier)
    {
        try
        {
            var codeInfo = await AuthenticationService.Instance.GenerateSignInCodeAsync(identifier);
            // Display code, and expiration and identifier to the player
        }
        catch (Exception e)
        {
            // Notify the client something went wrong generating the code
        }
    }
  3. Give player access to input the SignInCode, in a second already authenticated device.

  4. (Optional) Use the player-provided SignInCode to fetch the SignInCode information, using GetSignInCodeInfoAsync, and display it to the player to confirm they're authorizing the correct device by comparing the identifier on both devices.

  5. Authorize the Code-Link sign in using ConfirmCodeAsync.

    Note: The ConfirmCodeAsync method also supports optional parameters in idProvider and externalToken. These parameters should only be used in consoles and returns an error if used from console clients.

    async Task GetCodeInfoAsync(string signInCode)
    {
        try
        {
            var codeInfo = await AuthenticationService.Instance.GetSignInCodeInfoAsync(signInCode);
            // Display code, and expiration and identifier to the player and ask them to authorize the code-link sign in
        }
        catch (Exception e)
        {
            // Notify the client something went wrong getting the code information
        }
    }
    
    async Task ConfirmCodeAsync(string signInCode)
    {
        try
        {
            await AuthenticationService.Instance.ConfirmCodeAsync(signInCode);
            // Display a confirmation that the device should be authorized
        }
        catch (Exception e)
        {
            // Notify the client something went wrong with the code confirmation
        }
    }
  6. Call SignInWithCodeAsync to sign in the player on the same device that generated the SignInCode. (The credentials to validate the Code-Link sign in are stored in memory so make sure to call GenerateSignInCodeAsync before calling SignInWithCodeAsync or it will throw an exception). If the code has yet to be authorized, this method is successful, but the player won't be signed in; look for the SignedIn event to see if the sign in was successful.

    public async Task SignInWithCodeAsync()
    {
        try
        {
            AuthenticationService.Instance.SignedIn += UserHasSignedIn
            await AuthenticationService.Instance.SignInWithCodeAsync();
        }
        catch (Exception e)
        {
            // Notify the client something went wrong signing in the player
        }
        finally
        {
            AuthenticationService.Instance.SignedIn -= UserHasSignedIn
        }
    }
    
    public void UserHasSignedIn() {
        // Notify the client the user signed in successfully.
    }

    Note: The SignInWithCodeAsync method also allows for automatic polling (every five seconds), so the device automatically signs the player in after the code has been authorized, or throws an exception if the code has expired. This method also accepts a CancellationToken to cancel the polling.

    CancellationTokenSource CodeLinkCancellationTokenSource;
    
    public async Task PollSignInWithCodeAsync()
    {
        try
        {
            CodeLinkCancellationTokenSource = new CancellationTokenSource();
            await AuthenticationService.SignInWithCodeAsync(true, CodeLinkCancellationTokenSource.Token);
        }
        catch (Exception e)
        {
            // Notify the client something went wrong with Polling for Code Confirmation
        }
    }
    
    public void CancelSignInWithCode()
    {
        CodeLinkCancellationTokenSource.Cancel();
    }