Skip to main content

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

Last updated on March 22, 2024

Connect the UI to let player log in with device ID

In this tutorial, you will learn how to connect the login menu user interface (UI) with the login methods you implemented previously. You are going to get the AuthEssentialsWrapper_Starter class reference, set up the callback function OnLoginCompleted in LoginHandler_Starter, and start the login process from the existing LoginHandler.Login() method you created.

  1. Open Byte Wars in Unity. From Project window, open /Assets/Resources/Modules/AuthEssentials/Scripts/UI/LoginHandler_Starter.cs.

  2. Create a new callback function for OnLoginCompleted in the LoginHandler_Starter file. Based on the ResultCallback, set to change the screen to the MainMenuCanvas using the in-game utility MenuManager on login success and set to change the view to LoginFailed on login failed.

    private void OnLoginCompleted(Result<TokenData, OAuthError> result)
    {
    if (!result.IsError)
    {
    MenuManager.Instance.ChangeToMenu(AssetEnum.MainMenuCanvas);
    }
    else
    {
    failedMessageText.text = "Login Failed: " + result.Error.error;
    CurrentView = LoginView.LoginFailed;
    }
    }
  3. To access the AuthEssentialsWrapper_Starter class, you can use TutorialModuleManager to get the reference easily, then store it to a local variable in the script's Start().

    private AuthEssentialsWrapper_Starter _authWrapper;
    private void Start()
    {
    _authWrapper = TutorialModuleManager.Instance.GetModuleClass<AuthEssentialsWrapper_Starter>();
    }
  4. To enable retrying logins with the same method, prepare a local variable to store the last login method.

    private LoginType _lastLoginMethod;
  5. Update the LoginHandler_Starter.Login() function by adding the LoginType as a parameter. Inside the function, store the login method and call the AuthEssentialsWrapper_Starter.Login() right after changing the view to LoginLoading you created previously.

    private void Login(LoginType loginMethod)
    {
    CurrentView = LoginView.LoginLoading;
    _lastLoginMethod = loginMethod;
    _authWrapper.Login(loginMethod, OnLoginCompleted);
    }
  6. Since you updated the Login() function, you also need to update the Login() function calls inside OnLoginWithDeviceIdButtonClicked() and OnRetryLoginButtonClicked() that are listening to the onClick event of loginWithDeviceIdButton and quitGameButton, respectively, that you created earlier.

    private void OnLoginWithDeviceIdButtonClicked()
    {
    Login(LoginType.DeviceId);
    }
    private void OnRetryLoginButtonClicked()
    {
    Login(_lastLoginMethod);
    }