Use the SDK for Steam login - Login with Steam - (Unity module)
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
.
You will use
steamAuthSessionTicket
as aplatformToken
and show retry pop-up if it somehow fails to getsteamAuthSessionTicket
. Enter the code below intoSinglePlatformAuthWrapper_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);
}
}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 callLoginHandler.OnLoginCompleted
later when you have successfully obtained the public user data. Enter the highlighted code below intoSinglePlatformAuthWrapper_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);
}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 cachedTokenData
. Enter the highlighted code below intoSinglePlatformAuthWrapper_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));
}
}Add the
CreateLoginErrorResult
function, which is needed on login complete to handle the error scenarios. Enter the code below intoSinglePlatformAuthWrapper_Starter
.private Result<TokenData, OAuthError> CreateLoginErrorResult(ErrorCode errorCode, string errorDescription)
{
return Result<TokenData, OAuthError>.CreateError(new OAuthError()
{
error = errorCode.ToString(),
error_description = errorDescription
});
}
Resources
- The file used in this tutorial section is available in the Unity Byte Wars GitHub repository.