Skip to main content

Put it all together - Login with Steam - (Unreal Engine module)

Last updated on April 9, 2024

Connect the UI to let player log in with Steam

In this tutorial, you will learn how to connect the SinglePlatformAuthWidget_Starter class with the AuthEssentialsSubsystem_Starter subsystem to handle the Steam login process.

  1. Open the SinglePlatformAuthWidget_Starter C++ class and ensure that your class has the following code in the NativeOnActivated() function.

    void USinglePlatformAuthWidget_Starter::NativeOnActivated()
    {
    Super::NativeOnActivated();

    if (!AuthEssentialsModule->IsStarterModeActive())
    {
    UE_LOG_AUTH_ESSENTIALS(Warning, TEXT("SinglePlatformAuth is dependent to AuthEssentials, please enable starter mode for the AuthEssentials module."))
    Btn_LoginWithSinglePlatformAuth->SetVisibility(ESlateVisibility::Collapsed);
    return;
    }

    const bool bIsNativePlatformValid = IOnlineSubsystem::GetByPlatform() != nullptr;
    if (!bIsNativePlatformValid)
    {
    Btn_LoginWithSinglePlatformAuth->SetVisibility(ESlateVisibility::Collapsed);
    return;
    }

    if (ShouldAutoLogin())
    {
    OnLoginWithSinglePlatformAuthButtonClicked();
    return;
    }

    const FString LoginButtonText = TEXT_LOGIN_WITH.ToString().Replace(TEXT("%PLATFORM%"), *GetDefaultNativePlatform());
    Btn_LoginWithSinglePlatformAuth->SetButtonText(FText::FromString(LoginButtonText));
    Btn_LoginWithSinglePlatformAuth->OnClicked().AddUObject(this, &ThisClass::OnLoginWithSinglePlatformAuthButtonClicked);
    LoginWidget->SetButtonLoginVisibility(ShouldDisplayDeviceIdLogin() ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
    }
  2. In the same class, add the following code to the OnLoginWithSinglePlatformAuthButtonClicked() function.

    void USinglePlatformAuthWidget_Starter::OnLoginWithSinglePlatformAuthButtonClicked()
    {
    UAuthEssentialsSubsystem_Starter* AuthSubsystem = GetGameInstance()->GetSubsystem<UAuthEssentialsSubsystem_Starter>();
    ensure(AuthSubsystem);

    // Set the login widget to logging in state and bind the retry login delegate.
    LoginWidget->SetLoginState(ELoginState::LoggingIn);
    LoginWidget->OnRetryLoginDelegate.AddUObject(this, &ThisClass::OnLoginWithSinglePlatformAuthButtonClicked);

    // Login with single platform auth is considered as login with default native platform.
    // Thus, it doesn't need username, token, nor the login method.
    AuthSubsystem->SetAuthCredentials(EAccelByteLoginType::None, TEXT(""), TEXT(""));
    AuthSubsystem->Login(GetOwningPlayer(), FAuthOnLoginCompleteDelegate_Starter::CreateUObject(LoginWidget, &ULoginWidget_Starter::OnLoginComplete));
    }

When you click the Login with Steam button, it will perform a Steam login via UAuthEssentialsSubsystem_Starter. The callback is already being handled with the OnLoginComplete() function in ULoginWidget_Starter, which will do the following:

  • If the login is successful, navigate to the main menu.
  • If the login fails, display an error message.

Resources