Skip to navigation

Check session

After a successful sign-in, AuthlyX needs to check that access is still valid. A heartbeat is that repeated check while the app is running. In the default server mode, the SDK does it for you using the interval sent by AuthlyX. Most apps should keep this default.

checkSession is one check you can call yourself. Use it when your app returns from sleep, a long pause or a suspended mobile/desktop state. Do not call checkSession every few seconds when automatic heartbeat is already enabled.

Check again after the app wakes

onResume means “the app is active again, check access now.” It performs the same server check as checkSession. It does not start a new session or extend an expired one. Call it once when your app resumes after being asleep or suspended. The SDK also checks again on its normal heartbeat schedule. Some languages use their normal naming style, such as onResume in Python and Rust.

Default mode and manual mode

  • Server mode is the default. The SDK starts periodic checks after sign-in and handles session invalidation. Use this unless you have a reason to control scheduling yourself.
  • Manual mode turns off the automatic timer. Your app must call checkSession while it is active and onResume when it wakes. If you forget to call them, the app may keep showing protected screens until another request reaches AuthlyX.

The default is server, so most apps do not need to set this option. In this mode, the SDK starts its own timer after a successful sign-in and checks again at the interval returned by AuthlyX. If your app will manage the schedule itself, choose manual when creating the SDK instance:

LanguageSet manual mode
C#new AuthlyX(appKey, version, heartbeat: "manual")
C++auto options = authlyx::defaultOptions(appKey, version); options.heartbeat = "manual"; authlyx::AuthlyX app(std::move(options));
PythonAuthlyX(app_key, version, heartbeat="manual")
JavaScriptnew AuthlyX(appKey, version, { heartbeat: "manual" })
TypeScriptnew AuthlyX(appKey, version, { heartbeat: "manual" })
JavaAuthlyX.Options options = AuthlyX.defaults(appKey, version); options.heartbeat = "manual"; AuthlyX app = new AuthlyX(options);
GoSet Options.Heartbeat = "manual" before calling authlyx.New(ctx, options). Check the errors from both DefaultOptions and New.
Rustlet options = AuthlyX::defaults(app_key, version)?.withHeartbeat("manual")?; let app = AuthlyX::withOptions(options).await?;

These are construction examples only. Keep the normal AppKey, version, storage and trusted public keys in the SDK options. If you do not already have a reason to control the schedule, leave the setting alone.

When access ends

In server mode, you do not need to call checkSession on a timer. The SDK checks in the background. When AuthlyX confirms that the session has expired, was ended, or the identity is paused or banned, the SDK clears its signed-in state and calls the invalidation handler. Use that handler to hide protected screens and show your sign-in screen. The SDK does not close your app.

Each language names that handler differently. Set it when creating the SDK, except C#, where it is an event on the app object:

LanguageHandle a confirmed session end
C#app.SessionInvalidated += error => Console.WriteLine(error.Message);
C++Set options.on_invalidated before constructing authlyx::AuthlyX; print error.what() inside the handler.
PythonPass on_session_invalidated=lambda error: print(error.message) to AuthlyX(...).
JavaScript / TypeScriptPass { onSessionInvalidated: error => console.log(error.message) } as the third AuthlyX argument.
JavaSet options.onInvalidated = error -> System.out.println(error.getMessage()); before new AuthlyX(options).
GoSet options.OnInvalidated = func(err *authlyx.Error) { fmt.Println(err.Message) } before authlyx.New.
RustSubscribe to app.subscribe() and handle the Event::SessionInvalidated(error) event.

These handlers print the reason so the example works in a console. In a real app, replace the print call with your own code to show the sign-in screen. The SDK does not close your app. See the language tabs below for checking a session yourself.

A timeout is different from an expired session: the server did not answer, so the SDK cannot confirm that access ended. Show a connection problem and keep protected actions unavailable until access is confirmed again.

SDK version: V3 Beta. These examples use the current downloads. For what the callback, future, await, CompletableFuture and Go err mean, read Results.

app.checkSession(sessionResult =>
{
if (sessionResult.ok)
Console.WriteLine("Session is active");
else
Console.WriteLine("Sign in again: " + sessionResult.message);
});
async Task CheckAfterResume()
{
var resumeResult = await app.onResume();
if (!resumeResult.ok)
Console.WriteLine("Sign in again: " + resumeResult.message);
}

checkSession accepts a callback. onResume() returns a Task, so call it with await inside an async method. Call CheckAfterResume() from the event your framework raises when the app wakes. This SDK method does not have a callback overload for onResume.

The code above prints a simple message to keep the example short. In a real app, disable paid or protected actions when access is not confirmed, keep unrelated work available, and show your own sign-in or connection screen.