Skip to main content

Use the AGS Game SDK to log in - Login with device ID - (Unity module)

Last updated on June 12, 2024

Unwrap the wrapper

In this tutorial, you will learn how to implement device ID logins into your game using the AccelByte Gaming Services (AGS) Game SDK.

Byte Wars uses the AuthEssentialsWrapper class as a wrapper to cache and handle login-related functionalities with the AGS Game SDK. This provides modularity without overriding engine classes.

The AuthEssentialsWrapper class wraps the ApiClient and User class provided by the AGS Game SDK. The User class handles account-related interactions with AGS that provides the ability to authenticate users and obtain access tokens. After a successful authentication, it will allow you to utilize the AGS features as a user.

The diagram below explains how the AuthEssentialsWrapper connects the LoginHandler to use the AGS Game SDK functionalities.

Login with Device Id Flow Unity Byte Wars device ID

In this tutorial, you will learn how to call the Login function in User class from AuthEssentialsWrapper and set up the OnLoginCompleted callback to receive the login response from AGS.

What's in the Starter Pack

The starter class AuthEssentialsWrapper_Starter has been provided for you to modify. It is available in the Resources section.

  • AuthEssentialsWrapper_Starter file: /Assets/Resources/Modules/AuthEssentials/Scripts/AuthEssentialsWrapper_Starter.cs

The AuthEssentialsWrapper_Starter class already contains the AGS Game SDK libraries.

using AccelByte.Api;
using AccelByte.Core;
using AccelByte.Models;

AuthEssentialsModels is a file that holds any model data used in AuthEssentialsWrapper_Starter. It is available in the Resources section.

  • AuthEssentialsModels file: /Assets/Resources/Modules/AuthEssentials/Scripts/AuthEssentialsModels.cs

The AuthEssentialsModels contains LoginType, an enum that defines the type of login methods.

public enum LoginType
{
DeviceId
}

Implement logins using the AGS Game SDK

  1. Open Byte Wars in Unity.

  2. In AuthEssentialsWrapper_Starter.cs, copy the code below to get the AGS Game SDK's ApiClient that holds the reference to the User class to access login functionalities.

    // AccelByte's Multi Registry references
    private ApiClient apiClient;
    private User user;

    void Start()
    {
    apiClient = MultiRegistry.GetApiClient();
    user = apiClient.GetApi<User, UserApi>();
    }
  3. Declare a local property to store data of the current logged-in user, which can be accessed from another script later.

    public TokenData userData;
  4. Since the login process is asynchronous, create a callback function called OnLoginCompleted to receive the login response from AGS. To keep it modular, add the AGS Game SDK's Result and ResultCallback as parameters so the ResultCallback method can later proceed with the login result.

    private void OnLoginCompleted(Result<TokenData, OAuthError> result, ResultCallback<TokenData, OAuthError> customCallback = null)
    {
    if (!result.IsError)
    {
    Debug.Log("Login user successful.");
    userData = result.Value;
    }
    else
    {
    Debug.Log($"Login user failed. Message: {result.Error.error}");
    }

    customCallback?.Invoke(result);
    }
  5. Implement the Login method. Add local variables below to store the required values when calling the Login method.

    private PlatformType _platformType;
    private string _platformToken;
  6. Create a new function called Login inside AuthEssentialsWrapper_Starter with the LoginType enum and the AGS Game SDK's ResultCallback as parameters. Inside the function, set the _platformType to PlatformType.Device and the _platformToken to SystemInfo.deviceUniqueIdentifier. Then, call the LoginWithOtherPlatform() method in the User class with the provided platform type, platform token, and callback function as parameters.

    public void Login(LoginType loginMethod, ResultCallback<TokenData, OAuthError> resultCallback)
    {
    switch (loginMethod)
    {
    case LoginType.DeviceId:
    _platformType = PlatformType.Device;
    _platformToken = SystemInfo.deviceUniqueIdentifier;
    break;
    }

    Debug.Log("[AuthEssentialsWrapper] Trying to login with device id: "+ _platformToken);

    user.LoginWithOtherPlatform(_platformType, _platformToken, result => OnLoginCompleted(result, resultCallback));
    }

Resources