Skip to main content

Put it all together - Login with device ID - (Unity module)

Last updated on November 7, 2025

Connect the UI to Let the Player Log In with Device ID​

In this tutorial, you will learn how to connect the login menu with the login implementation created in the previous section.

  1. Open the LoginMenu_Starter class and define a variable to store the wrapper you created earlier:

    private AuthEssentialsWrapper_Starter authWrapper;
  2. Initialize the wrapper in the Start() function, alongside your button bindings:

    private void Start()
    {
    ...
    authWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
    loginWithDeviceIdButton.onClick.AddListener(OnLoginWithDeviceIdButtonClicked);
    quitGameButton.onClick.AddListener(OnQuitGameButtonClicked);
    }
  3. Update the OnLoginWithDeviceIdButtonClicked() function to send the login request using your wrapper:

    private void OnLoginWithDeviceIdButtonClicked()
    {
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Loading);
    OnRetryLoginClicked = OnLoginWithDeviceIdButtonClicked;
    authWrapper.LoginWithDeviceId(OnLoginCompleted);
    }
  4. Create the callback function to handle the login result for the UI:

    public void OnLoginCompleted(Result<TokenData, OAuthError> result)
    {
    if (!result.IsError)
    {
    MenuManager.Instance.ChangeToMenu(AssetEnum.MainMenuCanvas);
    BytewarsLogger.Log($"Login successful: {result.Value.ToJsonString()}");
    }
    else
    {
    widgetSwitcher.ErrorMessage = $"Login failed: {result.Error.error}";
    widgetSwitcher.SetWidgetState(AccelByteWarsWidgetSwitcher.WidgetState.Error);
    StartCoroutine(SetSelectedGameObject(retryLoginButton.gameObject));
    }
    }
  5. Now open the AuthEssentialsWrapper_Starter class and add a new callback function to handle lobby disconnection. If the disconnection is due to multiple sessions or an intentional logout, the game should log out the user:

    private void OnLobbyDisconnected(WsCloseCode code)
    {
    BytewarsLogger.Log($"Lobby service disconnected with code: {code}");

    /* If disconnected from lobby intentionally or due to account issue,
    * log out and return to the login menu. */
    HashSet<WsCloseCode> disconnectCode = new HashSet<WsCloseCode>()
    {
    WsCloseCode.Normal,
    WsCloseCode.DisconnectDueToMultipleSessions,
    WsCloseCode.DisconnectDueToIAMLoggedOut
    };

    if (disconnectCode.Contains(code))
    {
    lobby.Disconnected -= OnLobbyDisconnected;
    Logout(() => { MenuManager.Instance.ChangeToMenu(AssetEnum.LoginMenu); });
    }
    }
  6. Still in the same file, add the following code to the Awake() function to listen for lobby disconnection and quit actions:

    private void Awake()
    {
    ...
    lobby.Disconnected += OnLobbyDisconnected;
    MainMenu.OnQuitPressed += Logout;
    }

Resources​