Skip to content

Core session

The service that keeps the login session in memory. It holds the access token, refresh token, Player ID, and expiration time, and it raises events when the session changes or ends.

The session exists only in memory. It disappears when the app is relaunched, so to provide automatic login on the next launch, the app must save the tokens itself.

ISessionManager

interface — namespace Hive.Axyl.Core

  • Package: com.com2usplatform.hiveaxyl.core
  • Registration: Registered automatically as a Core default service

It is safe to call from any thread.

Retrieval

Because it is a Core default service, you can retrieve it without separate registration.

using Hive.Axyl.Core;

ISessionManager session = HiveCore.Resolve<ISessionManager>();

Method summary

Methods

SetSession

Registers a new session. If a session already exists, it is replaced. Call it after a successful login, or when you restore the session with saved tokens at app startup.

When automatic token refresh succeeds, the SDK calls it internally.

After the call, OnSessionRefreshed is raised.

void SetSession(string accessToken, string refreshToken, long playerId, long expiresAtSec)
Parameter Type Required Description
accessToken string Required The access token. It cannot be null.
refreshToken string Required The refresh token. It cannot be null.
playerId long Required The Player ID.
expiresAtSec long Required The expiration time of the access token, as a Unix timestamp in seconds (UTC).

Exceptions

  • ArgumentNullException: When accessToken or refreshToken is null

Call example

session.SetSession(accessToken, refreshToken, playerId, expiresAtSec);

For the session registration procedure after token issuance, see Register the session.

ClearSession

Deletes the current session and resets all values to their defaults. Call it when the app user logs out. This call is treated as the app's definitive judgment that "the session has ended."

The SDK also calls it internally when automatic refresh is rejected because the server definitively responded that the refresh token itself is invalid. In contrast, refresh failures without a definitive judgment, such as network failures, server errors, and cancellation, do not go through this method. Likewise, SDK shutdown resets the session silently.

After the session is deleted, OnSessionExpired is raised. If there is no active session, it does nothing and raises no event.

void ClearSession()

GetSnapshot

Gets a snapshot of the current session state.

SessionSnapshot GetSnapshot()

Events

Both events are invoked on the engine main thread, so you can use engine APIs inside the handlers.

The list of subscribers is fixed at the moment the event is raised. Therefore, a handler can be called one more time right after you unsubscribe it.

OnSessionExpired

Raised only when it is determined that the stored credentials can no longer be used, and it is a signal to delete the credentials you saved. For the criteria for when you can delete them and when you must preserve them, see Preserve stored credentials.

event Action OnSessionExpired

Whether it is raised in each situation is as follows.

  • When it is raised
    • When the app calls ClearSession() for an active session. This is usually a logout.
    • When automatic refresh is rejected because the server definitively responded that the refresh token itself is invalid, and the tokens are discarded
  • When it is not raised
    • When ClearSession() is called while there is no active session
    • When the SDK is shut down
    • Refresh failures where the token's validity could not be determined, such as network failures, server errors, and cancellation. It is not raised even when the session ends after three consecutive failures.
    • When the access token has simply expired. The SDK first tries automatic refresh.
The session can end without an event

A refresh failure without a definitive judgment leaves the session as is. However, if such failures happen three times in a row, the in-memory session ends silently. Attempts that end with cancellation are not counted.

In this case, OnSessionExpired is not raised, so IsLoggedIn can become false without an event. The credentials you saved are still valid, so you must not delete them.

At this point, you must register the session again instead of retrying. If you check IsLoggedIn again and then call SetSession() through the login or session restoration flow, automatic refresh works again.

OnSessionRefreshed

Raised every time a new session is registered or the tokens are replaced. This includes right after login or session restoration, and right after the SDK automatically refreshes an expired access token. The snapshot passed to it holds the newly stored values.

event Action<SessionSnapshot> OnSessionRefreshed
Parameter Type Description
— SessionSnapshot The newly stored session values.
Save the tokens in this event

A refresh request is treated as usable only once. After a refresh, the refresh token you saved earlier may no longer be usable, so update it with the new value and save it in this event.

Subscription example

using Hive.Axyl.Core;

ISessionManager session = HiveCore.Resolve<ISessionManager>();

session.OnSessionRefreshed += snapshot =>
{
    // Save the new tokens to the app storage, overwriting the old ones.
};

session.OnSessionExpired += () =>
{
    // Delete the saved tokens and go to the login screen.
};

Properties

Property Type Description
AccessToken string? The current access token. null if there is no active session.
RefreshToken string? The current refresh token. null if there is no active session.
PlayerId long The Player ID. 0 if the user is not logged in.
IsLoggedIn bool true if there is an active session. This is the same as AccessToken not being null.

Data types

SessionSnapshot

struct — namespace Hive.Axyl.Core

A value that holds the session state at a specific point in time. It is the return value of GetSnapshot() and the value passed to OnSessionRefreshed, and it cannot be changed after creation.

ToString() masks the tokens in its output, so the tokens are not exposed even if you write it to logs as is.

Property Type Required Description
AccessToken string? Optional The access token. null if there is no active session.
RefreshToken string? Optional The refresh token. null if there is no active session.
PlayerId long Required The Player ID. 0 if the user is not logged in.
ExpiresAtSec long Required The expiration time of the access token, as a Unix timestamp in seconds (UTC). 0 if there is no active session.