C++ integration

The following section shows how to integrate with the Authentication SDK using Unreal Engine Subsystems. The Blueprint API provides the Authentication Subsystem to expose this functionality.

Add the Authentication SDK as a dependency

Before continuing, add Authentication as a public dependency of your module, then include the plugin header files in your classes.

Add Authentication as a dependency of your module to your Unreal project build file:

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Authentication" });

Include the plugin header files you wish to access in your own classes:

#include "AuthenticationSubsystem.h"

Authentication Subsystem

The Authentication Subsystem contains the interface to communicate with Unity Authentication servers, hold player profile information, and load/unload player preferences from local storage. It’s responsible for handling the authentication lifecycle and storing important authentication information for your project.

You can access the Authentication Subsystem by obtaining a reference from a UGameInstance.

UWorld* GameWorld = GetWorld();
UGameInstance* GameInstance = GameWorld->GetGameInstance();
UAuthenticationSubsystem* AuthenticationSubsystem = GameInstance->GetSubsystem<UAuthenticationSubsystem>();

SignInAnonymously

Use the SignInAnonymously() method to anonymously authenticate. This is a quick way to authenticate without any user information, and requires no interaction with external providers. If successful, this populates the current player profile with the retrieved credentials returned from the Unity Authentication servers.

SignInAnonymously() takes an FAuthenticationSignInOptions struct as a parameter that changes the way a sign-in is performed. See the Unity API services documentation page for more information about these parameters.

The response from the SDK can be handled in a response handler which needs to take in an FAuthenticationResponse.

// Create sign-in options body
FAuthenticationSignInOptions SignInOptions;
SignInOptions.bCreateAccount = true;
SignInOptions.Nonce = TEXT("abc123");

AuthenticationSubsystem->SignInAnonymously(SignInOptions, THandler<FAuthenticationResponse>::CreateLambda([this](
FAuthenticationResponse Response)
{
	// Your response logic here
}));

GetUserInfo

Use the GetUserInfo() method to retrieve information about the currently authenticated user. This includes their user Id, authentication timestamps, and any external Identity providers that are linked to their session.

The response from the SDK can be handled in a response handler which needs to take in an FAuthenticationUserResponse.

AuthenticationSubsystem->GetUserInfo(
THandler<FAuthenticationUserResponse>::CreateLambda([this](
FAuthenticationUserResponse Response)
{
	// Your response logic here
}));

DeleteUser

Use the DeleteUser() method to delete all information related to the currently authenticated player. This method also signs out the player, and deletes all player preferences and profiles connected to the player.

Note: If DeleteUser() Is called while using the default profile, then the profile information is cleared and the profile stays intact.

The response from the SDK can be handled in a response handler which needs to take in a bool: its value is true in case of successful deletion and false otherwise.

AuthenticationSubsystem->DeleteUser(
THandler<bool>::CreateLambda([this](bool bResponse)
{
	// Your response logic here
}));

RegisterStateChangedCallback

Use the RegisterStateChangedCallback() method to assign a callback function that invokes upon the state of the subsystem changing. For instance, the assigned function executes when a player has successfully authenticated and the subsystem state has changed to Authorized.

The response from the SDK can be handled in a response handler which needs to take in an AuthenticationStateChangedResponse.

AuthenticationSubsystem->RegisterStateChangedCallback(
THandler<AuthenticationStateChangedResponse>::CreateLambda([this](
AuthenticationStateChangedResponse Response)
{
	// Your response logic here
}));

SignOut

Use the SignOut() method to sign-out of the currently authenticated player profile. This removes the current player profile and switches to the default profile. This method also has an optional parameter to remove any stored credentials associated with this player.

Note: If SignOut() is called while using the default profile, then the profile information is cleared and the profile stays intact.

AuthenticationSubsystem->SignOut(true); // Clear player credentials

SwitchProfile

Use the SwitchProfile() method to switch to or create a player profile.

Note: SwitchProfile() can only be called while signed-out. If a profile switch is invoked while authenticated, a warning is logged and nothing happens.

FString NewProfileName = FString(TEXT("new_profile_123"));
AuthenticationSubsystem->SwitchProfile(NewProfileName);

ProfileExists

Use the ProfileExists() method to check if a given profile exists in the current session.

Note: ProfileExists() cannot detect profiles from previous sessions that have not been re-created in the current session.

FString ProfileToCheckFor = FString(TEXT("new_profile_123"));
bool bExists = AuthenticationSubsystem->ProfileExists(ProfileToCheckFor);

GetCurrentProfileName

Use the GetCurrentProfileName() method to retrieve the name of the current player profile.

FString ProfileName = AuthenticationSubsystem->GetCurrentProfileName();

GetProfileNames

Use the GetCurrentProfileName() method to retrieve a list of all player profile names being used in the current session.

TArray<FString> ProfileNames = AuthenticationSubsystem->GetProfileNames();

RegisterProfileChangedCallback

Use the RegisterProfileChangedCallback() method to assign a callback function that invokes upon the player profile changing. For instance, the assigned function executes when SwitchProfile has executed successfully.

The response from the SDK can be handled in a response handler which needs to take in an AuthenticationPlayerProfileChangedResponse.

AuthenticationSubsystem->RegisterPlayerProfileChangedCallback(
THandler<AuthenticationPlayerProfileChangedResponse>::CreateLambda([this](
AuthenticationPlayerProfileChangedResponse Response)
{
	// Your response logic here
}));

RegisterProfileDeletedCallback

Use the RegisterProfileChangedCallback() method to assign a callback function that invokes upon a player profile getting removed from the current session. For instance, the assigned function executes when SignOut has executed successfully.

The response from the SDK can be handled in a response handler which needs to take in an AuthenticationPlayerProfileChangedResponse.

AuthenticationSubsystem->RegisterPlayerProfileChangedCallback(
THandler<AuthenticationPlayerProfileChangedResponse>::CreateLambda([this](
AuthenticationPlayerProfileChangedResponse Response)
{
	// Your response logic here
}));

IsSignedIn

Use the IsSignedIn() method to check whether the current player profile is signed-in. Being “Signed-In” is defined as being either Authorized or Expired.

bool bSignedIn = AuthenticationSubsystem->IsSignedIn();

IsAnonymous

Use the IsAnonymous() method to check whether the current player profile is signed-in anonymously. This should return true after executing SignInAnonymously successfully.

bool bAnonymous = AuthenticationSubsystem->IsAnonymous();

IsAuthorized

Use the IsAuthorized() method to check whether the current player profile is signed-in and currently authorized. This should return true after executing any sign-in method successfully while the expiration time has not yet passed.

bool bAuthorized = AuthenticationSubsystem->IsAuthorized();

IsExpired

Use the IsExpired() method to check whether the current player profile’s session has expired. This should return true when the expiry time returned from a successful sign-in response has passed.

bool bExpired = AuthenticationSubsystem->IsExpired();

SessionTokenExists

Use the SessionTokenExists() method to check whether a session token exists in player preferences for the current player profile.

bool bTokenExists = AuthenticationSubsystem->SessionTokenExists();

GetUnityProjectId

Use the GetUnityProjectId() method to retrieve the Unity Project Id associated with the current authentication session.

FGuid ProjectId = AuthenticationSubsystem->GetUnityProjectId();

GetUnityEnvironmentName

Use the GetUnityEnvironmentName() method to retrieve the name of the Unity Environment Id associated with the current authentication session.

FString EnvironmentId = AuthenticationSubsystem->GetUnityEnvironmentName();

GetAccessToken

Use the GetAccessToken() method to retrieve the access token for the current session. If none exists, this returns an empty string.

FString AccessToken = AuthenticationSubsystem->GetAccessToken();

GetSessionToken

Use the GetSessionToken() method to retrieve the session token for the current session. If none exists, this returns an empty string.

FString SessionToken = AuthenticationSubsystem->GetSessionToken();

GetUserId

Use the GetUserId() method to retrieve the user Id for the current session. If none exists, this returns an empty string.

Note: This is different from the player profile name. The user Id is the unique user identifier returned from the Unity Authentication System.

FString UserId = AuthenticationSubsystem->GetUserId();

GetState

Use the GetState() method to retrieve the current state of the authentication session.

EAuthenticationState UserId = AuthenticationSubsystem->GetUserId();

SetUnityProjectId

Use the SetUnityProjectId() method to set the Unity Project Id for the current authentication session. This overrides the Unity Project Id configured in Project Settings.

FGuid NewProjectId = FGuid(TEXT("00000000-1234-1234-000000000000"));
AuthenticationSubsystem->SetUnityProjectId(NewProjectId);

SetUnityEnvironmentName

Use the SetUnityEnvironmentName() method to set the Unity Environment name for the current authentication session. This overrides the Unity Environment name configured in Project Settings.

FString NewEnvironmentName = FString(TEXT("testenv2"));
AuthenticationSubsystem->SetUnityEnvironmentName(NewEnvironmentName );