Documentation

Support

Authentication

Code Link

Provide a Code Link sign-in option to enable account linking by sharing unique codes between devices.
Read time 2 minutesLast updated 15 hours ago

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.

Set up 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 using Code-Link

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