Skip to main content

Logging in and out, and retrieving local user information

Once you've configured authentication in your project, you can now log users in when your game launches.

Login a user

It's recommended that you start the login process when the game launches. The login process might display interactive prompts to the user, so you'll usually want to display some sort of loading or splash screen during this process.

caution

You can not call AutoLogin immediately inside the GameInstance Init event, because the first UWorld is not yet ready. Since the world isn't ready, the login process can't display interactive prompts to the user.

If you want to login during Init, add a delay (such as a Delay node if you are using blueprints) to make the AutoLogin call happen on the next frame.

If you are using C++ and not blueprints, you'll need to update your game's .Build.cs file to reference the OnlineSubsystem and OnlineSubsystemUtils modules. Add the following lines to your .Build.cs file:

PublicDependencyModuleNames.AddRange(new string[]
{
"OnlineSubsystem",
"OnlineSubsystemUtils",
});

Then, get the online identity interface:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
#include "Interfaces/OnlineIdentityInterface.h"

// ...

IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();

Register the event handler so you know when login is complete. LoginDelegateHandle is declared as an FDelegateHandle.

this->LoginDelegateHandle = Identity->AddOnLoginCompleteDelegate_Handle(
0,
FOnLoginComplete::FDelegate::CreateUObject(
this,
&UMyClass::HandleLoginComplete));

Then call AutoLogin. If you're running on a console where there might be multiple local users, you use the LocalUserNum parameter to log each of them in.

if (!Identity->AutoLogin(0 /* LocalUserNum */))
{
// Call didn't start, return error.
}

When your callback fires, you'll want to handle any errors (check bWasSuccessful), and then deregister the event handler:

void UMyClass::HandleLoginComplete(
int32 LocalUserNum,
bool bWasSuccessful,
const FUniqueNetId &UserId,
const FString &Error)
{
// TODO: Check bWasSuccessful to see if the login was completed.

// Deregister the event handler.
IOnlineSubsystem *Subsystem = Online::GetSubsystem(this->GetWorld());
IOnlineIdentityPtr Identity = Subsystem->GetIdentityInterface();
Identity->ClearOnLoginCompleteDelegate_Handle(LocalUserNum, this->LoginDelegateHandle);
this->LoginDelegateHandle.Reset();
}

Logout a user

Most games won't need to implement logout functionality, especially if you intend for login to be happening non-interactively via store or platform authentication (Steam, etc.)

However, if you need to implement logout functionality so the user can sign in with a different account, you can use the Logout function on the identity interface.

The process for calling logout for the local user is basically the same as login, except that you call Logout instead of AutoLogin, and you register/deregister with AddOnLogoutCompleteDelegate_Handle and ClearOnLogoutCompleteDelegate_Handle instead. In addition, the callback handler should only have the (int32 LocalUserNum, bool bWasSuccessful) parameters.

Detecting if a user is logged in

Use GetLoginStatus to detect if a user is logged in.

Get a user's unique net ID

Call GetUniquePlayerId to get the unique ID of a local user.

Get a user's display name

To quickly get a local user's display name, you can use GetPlayerNickname.

Get a user's account information

To get the full account information for an authenticated user, including accessing the user's attributes, use GetUserAccount. This requires the user's unique net ID, so call GetUniquePlayerId to get that value first.

If you need to iterate through all of the user accounts that are logged in, you can use GetAllUserAccounts which returns all authenticated users.