Skip to main content

Use the SDK for Steam login - Login with Steam - (Unity module)

Last updated on November 25, 2024
info

This tutorial module does not apply to WebGL builds due to limitations in Steamworks.

Unwrap the wrapper

This tutorial shows you how to use the AccelByte Gaming Services (AGS) Game SDK to log in with Steam session token. That way, players do not need to manually enter credentials when logging in to the game, but instead can rely on their Steam credentials to login automatically.

This tutorial uses a wrapper class named SinglePlatformAuthWrapper that acts as the wrapper to cache and handle login-related functionalities when using the AGS Game SDK and Steamworks.NET-related functions.

  • SinglePlatformAuthWrapper_Starter file: Assets/Resources/Modules/SinglePlatformAuth/Scripts/SinglePlatformAuthWrapper_Starter.cs

Implement logins using AGS Game SDK and Steam Session tokens

You will continue the Steam login implementation within SinglePlatformAuthWrapper_Starter.

  1. You will use steamAuthSessionTicket as a platformToken and show retry pop-up if it somehow fails to get steamAuthSessionTicket. Enter the code below into SinglePlatformAuthWrapper_Starter:

    private void OnGetAuthSessionTicketFinished(string steamAuthSessionTicket)
    {
    if (loginHandler == null) return;
    if (String.IsNullOrEmpty(steamAuthSessionTicket))
    {
    loginHandler.OnLoginCompleted(
    CreateLoginErrorResult(ErrorCode.CachedTokenNotFound,
    "Failed to get steam token"));
    }
    else
    {
    //login using steamAuthSessionTicket
    user.LoginWithOtherPlatform(PlatformType,
    steamAuthSessionTicket, OnLoginWithOtherPlatformCompleted);
    }
    }
  2. Upon success of a login with another platform, you will get the public user data, such as an avatar URL and a display name. You cache TokenData since you will call LoginHandler.OnLoginCompleted later when you have successfully obtained the public user data. Enter the highlighted code below into SinglePlatformAuthWrapper_Starter.

    private void OnLoginWithOtherPlatformCompleted(Result<TokenData, OAuthError> result)
    {
    if (result.IsError)
    {
    loginHandler.OnLoginCompleted(result);
    }
    else
    {
    tokenData = result.Value;
    GetUserPublicData(tokenData.user_id);
    }
    }

    private void GetUserPublicData(string receivedUserId)
    {
    GameData.CachedPlayerState.playerId = receivedUserId;
    user.GetUserByUserId(receivedUserId, OnGetUserPublicDataFinished);
    }
  3. Once the public user data has been successfully acquired, you can complete the Steam login flow and show the main menu. You can complete it by calling LoginHandler.OnLoginCompleted and pass the cached TokenData. Enter the highlighted code below into SinglePlatformAuthWrapper_Starter.

    private void OnGetUserPublicDataFinished(Result<PublicUserData> result)
    {
    if (result.IsError)
    {
    Debug.Log($"[{ClassName}] error OnGetUserPublicDataFinished:{result.Error.Message}");
    loginHandler.onRetryLoginClicked = () => GetUserPublicData(tokenData.user_id);
    loginHandler.OnLoginCompleted(CreateLoginErrorResult(result.Error.Code, result.Error.Message));
    }
    else
    {
    var publicUserData = result.Value;
    GameData.CachedPlayerState.avatarUrl = publicUserData.avatarUrl;
    GameData.CachedPlayerState.playerName = publicUserData.displayName;
    loginHandler.OnLoginCompleted(Result<TokenData,OAuthError>.CreateOk(tokenData));
    }
    }
  4. Add the CreateLoginErrorResult function, which is needed on login complete to handle the error scenarios. Enter the code below into SinglePlatformAuthWrapper_Starter.

    private Result<TokenData, OAuthError> CreateLoginErrorResult(ErrorCode errorCode, string errorDescription)
    {
    return Result<TokenData, OAuthError>.CreateError(new OAuthError()
    {
    error = errorCode.ToString(),
    error_description = errorDescription
    });
    }

Resources