Skip to main content

Use the online subsystem to log in - Login with Steam - (Unreal Engine module)

Last updated on October 24, 2024

Review the subsystem

In the Login with device ID module, you set up a subsystem called AuthEssentialsSubsystem_Starter for device ID login. Since the Steam login method has similar functionalities, you can reuse the subsystem to handle the Steam login method.

You will start by reviewing the AuthEssentialsSubsystem_Starter class and its functions.

  1. Inside the AuthEssentialsSubsystem_Starter class, you will find the SetAuthCredentials() function. This function is used to set the credentials for the login process through the parameters LoginMethod, Id, and Token.

    void UAuthEssentialsSubsystem_Starter::SetAuthCredentials(const EAccelByteLoginType& LoginMethod, const FString& Id, const FString& Token) 
    {
    Credentials.Type = (LoginMethod == EAccelByteLoginType::None) ? TEXT("") : FAccelByteUtilities::GetUEnumValueAsString(LoginMethod);
    Credentials.Id = Id;
    Credentials.Token = Token;
    }
  2. After calling the the function above, you can call the Login() function. This function takes the credentials set in the previous function and handles the login process using AccelByte Gaming Services (AGS) Online Subsystem.

    void UAuthEssentialsSubsystem_Starter::Login(const APlayerController* PC, const FAuthOnLoginCompleteDelegate& OnLoginComplete)
    {
    if (!ensure(IdentityInterface.IsValid()))
    {
    FString Message = TEXT("Cannot login. Identity interface is not valid.");
    UE_LOG_AUTH_ESSENTIALS(Warning, TEXT("%s"), *Message);
    OnLoginComplete.ExecuteIfBound(false, *Message);
    return;
    }

    const ULocalPlayer* LocalPlayer = PC->GetLocalPlayer();
    ensure(LocalPlayer != nullptr);
    int32 LocalUserNum = LocalPlayer->GetControllerId();

    IdentityInterface->AddOnLoginCompleteDelegate_Handle(LocalUserNum, FOnLoginCompleteDelegate::CreateUObject(this, &UAuthEssentialsSubsystem_Starter::OnLoginComplete, OnLoginComplete));
    IdentityInterface->Login(LocalUserNum, Credentials);

    /*
    * Logout On Game Exit
    * Workaround for the lobby not properly disconnecting when closing the PIE game.
    */
    if (UAccelByteWarsGameInstance* ByteWarsGameInstance = Cast<UAccelByteWarsGameInstance>(GetGameInstance()); ensure(ByteWarsGameInstance))
    {
    ByteWarsGameInstance->OnGameInstanceShutdownDelegate.AddWeakLambda(this, [this, LocalUserNum]()
    {
    IdentityInterface->Logout(LocalUserNum);

    UE_LOG_AUTH_ESSENTIALS(Warning, TEXT("Logging out local player %d"), LocalUserNum);
    });
    }
    }
  3. The OnLoginComplete() function is called when the login process is completed. This function will execute the OnLoginComplete delegate with the login result.

    void UAuthEssentialsSubsystem_Starter::OnLoginComplete(int32 LocalUserNum, bool bLoginWasSuccessful, const FUniqueNetId& UserId, const FString& LoginError, const FAuthOnLoginCompleteDelegate OnLoginComplete)
    {
    if (bLoginWasSuccessful)
    {
    UE_LOG_AUTH_ESSENTIALS(Log, TEXT("Login user successful."));
    }
    else
    {
    UE_LOG_AUTH_ESSENTIALS(Warning, TEXT("Login user failed. Message: %s"), *LoginError);
    }

    IdentityInterface->ClearOnLoginCompleteDelegates(LocalUserNum, this);
    OnLoginComplete.ExecuteIfBound(bLoginWasSuccessful, LoginError);
    }

The functions above are used to handle the login process for the device ID login method in the Login with device ID module. You will reuse these functions to handle the Steam login method in the next tutorial section.

Resources