Skip to content

Switch between accounts

To use multiple accounts alternately on one device, you must store the credentials of each account separately and then switch to the session of the account that the user selects. ISessionManager in the Hive Axyl SDK keeps only one session in memory, so when you switch accounts, the previous session is replaced by the new session.

The procedure for restoring the session is the same as in Automatic login. The difference is that you store credentials separately for each account and designate the account that the user selects as the one to restore.

Note

ISecureStorage saves and loads one value per key and does not provide a way to list all stored keys. In the app client, manage your own list that records which accounts are currently stored.

1. Save the credentials of the current account

Before switching accounts, store the credentials so that the user can return to the currently logged-in account. Save them right after the user finishes logging in or when the user enters the account switching screen.

Use separate keys for each account, and also record the list of stored Player IDs under a separate key.

using Hive.Axyl.Core;
using Hive.Axyl.Storage;

// Secure storage is not registered in unsupported environments such as the Editor, so check with TryResolve.
if (!HiveCore.TryResolve<ISecureStorage>(out var storage))
{
    return; // Without storage, do not store per-account credentials.
}
ISessionManager session = HiveCore.Resolve<ISessionManager>();

SessionSnapshot snapshot = session.GetSnapshot();
long playerId = snapshot.PlayerId;

// Save with per-account keys. The keys are examples of values the app chose (the SDK does not define them).
await storage.SaveAsync(new SecureStorageSaveRequest {
    Key   = $"hive.axyl.auth.account.{playerId}.access_token",
    Value = snapshot.AccessToken,
});
await storage.SaveAsync(new SecureStorageSaveRequest {
    Key   = $"hive.axyl.auth.account.{playerId}.refresh_token",
    Value = snapshot.RefreshToken,
});

// Also add this Player ID to the list of stored accounts.
await storage.SaveAsync(new SecureStorageSaveRequest {
    Key   = "hive.axyl.auth.account_index",
    Value = string.Join(",", accountIds),
});

If saving fails, do not add the Player ID to the list. If the list contains an account that has no credentials, switching to it keeps failing on the switching screen.

2. Build the list of stored accounts

When the user enters the account switching screen, read the list of stored accounts and display it on the screen. The Hive Axyl SDK has no method that lists stored accounts, so use the list you recorded in step 1 as is.

var loaded = await storage.LoadAsync(new SecureStorageLoadRequest {
    Key = "hive.axyl.auth.account_index",
});

if (loaded is SecureStorageLoadResult.Success ok && !string.IsNullOrEmpty(ok.Data.Value))
{
    string[] accountIds = ok.Data.Value.Split(',');
    // Build the screen that lists the switchable accounts with accountIds.
}
else
{
    // There are no stored accounts, so guide the user to log in with a new account.
}

The Hive Axyl SDK does not store additional information such as an account's display name. To show information other than the Player ID on the list screen, save it yourself in the app client.

3. Switch to the selected account

When the user selects an account from the list, load that account's credentials and replace the session. The restoration procedure is the same as the session restoration in Automatic login; only the keys to load change to those of the selected Player ID.

  1. Save the credentials of the currently logged-in account in the same way as in step 1.
  2. Load the tokens saved under the keys of the selected Player ID from ISecureStorage.
  3. Call LoginWithAccessTokenAsync() with the loaded access token, and if it fails, call IssueTokenAsync() with the refresh token.
  4. Call SetSession() with the newly issued tokens to replace the session.
  5. Reload the app data based on the switched account and update the screen.

If restoration fails, guide the user to run that account's login flow from the beginning. For external authentication provider accounts, see Log in with an external authentication provider. For guest accounts, see Log in as a guest.

The previous session remains as is until you call SetSession(). If restoration fails, do not change the session; let the user keep using the app with the original account.

4. Delete stored account information

Remove accounts that the user will no longer use on this device from the list. This only deletes the credentials stored on the device; the account on the Hive Axyl authentication server remains.

var deletedAccess = await storage.DeleteAsync(new SecureStorageDeleteRequest {
    Key = $"hive.axyl.auth.account.{targetPlayerId}.access_token",
});
var deletedRefresh = await storage.DeleteAsync(new SecureStorageDeleteRequest {
    Key = $"hive.axyl.auth.account.{targetPlayerId}.refresh_token",
});

if (deletedAccess is SecureStorageDeleteResult.Success
    && deletedRefresh is SecureStorageDeleteResult.Success)
{
    // Also remove targetPlayerId from the list of stored accounts and save the list again.
}

If deletion fails, do not remove the account from the list either. If the storage and the list do not match, the next switch attempt produces unexpected results.

To remove an account completely, you must delete the account itself, not just the stored information. To delete it, first switch to that account's session in step 3, and then see Delete an account.