Relogin using cached refresh token
Overview
The AccelByte Game SDK allows game developers to implement the relogin feature so that users in a game can remain logged in using a cached refresh token. This capability is useful to authenticate a player when a third-party service is down. We currently support this in the Windows platform for Steam, which allows a user to stay logged in and authenticated when the Steam service is down or under maintenance. The user can be logged in using the refresh token for the next 24 hours (one day).
Implementation
The AccelByte Game SDK logs the player in successfully first using the LoginWithOtherPlatform
function from the AccelByte User API. If the login succeeds, then the refresh token will be generated and cached automatically. The caching does not need an explicit call. After that point, the ReloginWithOtherPlatform
function can be used when the platform service is down or unavailable, and the relogin API will use the last generated platform refresh_token
to log in to the AccelByte platform.
Examples of the same can be seen below.
- Unreal
- Unreal OSS
- Unity
//Try to login using Steam first
FApiClientPtr ApiClient = FMultiRegistry::GetApiClient();
FString SteamTicket = GetSteamTicket(); // using UnrealEngine Online Subsystem Steam
bool bLoginDone = false;
bool bLoginUsingSteamAccountSuccess = false;
ApiClient->User.LoginWithOtherPlatform(EAccelBytePlatformType::Steam, SteamTicket,
FVoidHandler::CreateLambda([&]()
{
bLoginDone = true;
bLoginUsingSteamAccountSuccess = true;
}),
FErrorHandler::CreateLambda([&](int32 Code, FString Message)
{
bLoginDone = true;
}));
WaitUntil([&]() { return bLoginDone; }, "Waiting...");
FString SteamID = GetSteamUserId(); // using Steamworks
/// EXAMPLE:
/// FString GetSteamUserId()
/// {
/// SteamID SteamID = SteamUser()->GetSteamID();
/// uint64 SteamIdLong = SteamID.ConvertToUint64();
/// return FString::Printf(TEXT("%llu"), SteamIdLong);
/// }
if (bLoginUsingSteamAccountSuccess)
{
return;
}
// When login using Steam account failed due to any reason (i.e. Steam API is down)
// Use this example to relogin using cached refresh token
bool bReloginDone = false;
bool bReloginSuccess = false;
ApiClient->User.TryRelogin(SteamID,
FVoidHandler::CreateLambda([&]()
{
bReloginDone = true;
bReloginSuccess = true;
}),
FErrorHandler::CreateLambda([&](int32 Code, FString Message)
{
bReloginDone = true;
}));
WaitUntil([&]() { return bReloginDone; }, "Waiting...");
/// EXAMPLE: including Steamworks to your project .build.cs
/// FString GetSteamUserId()
/// {
/// SteamID SteamID = SteamUser()->GetSteamID();
/// uint64 SteamIdLong = SteamID.ConvertToUint64();
/// return FString::Printf(TEXT("%llu"), SteamIdLong);
/// }
FString SteamUserID = GetSteamUserId(); //Example of SteamUserID
int32 PlayerInputIndex = GetInputIndex(); //Example of the index
FOnlineAccountCredentials LoginCreds;
LoginCreds.Type = "CachedToken";
LoginCreds.Id = SteamUserID;
const IOnlineSubsystem* Subsystem = Online::GetSubsystem(GetWorld());
const IOnlineIdentityPtr IdentityInterface = Subsystem->GetIdentityInterface();
IdentityInterface->Login(PlayerInputIndex, LoginCreds);
using AccelByte.Core;
using AccelByte.Models;
using UnityEngine;
public class Main: MonoBehaviour
{
void Start()
{
string steamTicket = GenerateSteamTicket();
AccelByte.Api.User user = AccelByte.Core.MultiRegistry.GetApiClient().GetUser();
Result<TokenData, OAuthError> callback = null;
user.LoginWithOtherPlatform(AccelByte.Models.PlatformType.Steam, steamTicket, loginResult =>
{
callback = loginResult;
if (loginResult.IsError)
{
user.ReloginWithOtherPlatform(PlatformType.Steam, reloginResult =>
{
if (reloginResult.IsError)
{
Debug.logError(reloginResult.Error.error_description);
}
else
{
Debug.Log("Relogin success");
}
});
}
else
{
Debug.Log("Login success");
}
});
}
public static string GenerateSteamTicket()
{
var stringBuilder = new System.Text.StringBuilder();
if (SteamManager.Initialized)
{
var ticket = new byte[1024];
uint actualTicketLength;
Steamworks.SteamUser.GetAuthSessionTicket(ticket, ticket.Length, out actualTicketLength);
System.Array.Resize(ref ticket, (int)actualTicketLength);
foreach (byte stringByte in ticket)
{
stringBuilder.AppendFormat("{0:x2}", stringByte);
}
}
string steamTicket = stringBuilder.ToString();
return steamTicket;
}
}