> This page is for version V3 - Beta (default).
> For other versions, use one of these documentation indexes:
> - V3 - Beta (default): https://docs.authlyx.dev/v3/llms.txt
> - V2 - Legacy: https://docs.authlyx.dev/v2/llms.txt
> - V1 - End of support: https://docs.authlyx.dev/v1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.authlyx.dev/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.authlyx.dev/_mcp/server.

# Check session

> Check whether the current sign-in is still valid.

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:

| Language   | Set 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));`    |
| Python     | `AuthlyX(app_key, version, heartbeat="manual")`                                                                                       |
| JavaScript | `new AuthlyX(appKey, version, { heartbeat: "manual" })`                                                                               |
| TypeScript | `new AuthlyX(appKey, version, { heartbeat: "manual" })`                                                                               |
| Java       | `AuthlyX.Options options = AuthlyX.defaults(appKey, version); options.heartbeat = "manual"; AuthlyX app = new AuthlyX(options);`      |
| Go         | Set `Options.Heartbeat = "manual"` before calling `authlyx.New(ctx, options)`. Check the errors from both `DefaultOptions` and `New`. |
| Rust       | `let 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:

| Language                | Handle 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. |
| Python                  | Pass `on_session_invalidated=lambda error: print(error.message)` to `AuthlyX(...)`.                           |
| JavaScript / TypeScript | Pass `{ onSessionInvalidated: error => console.log(error.message) }` as the third `AuthlyX` argument.         |
| Java                    | Set `options.onInvalidated = error -> System.out.println(error.getMessage());` before `new AuthlyX(options)`. |
| Go                      | Set `options.OnInvalidated = func(err *authlyx.Error) { fmt.Println(err.Message) }` before `authlyx.New`.     |
| Rust                    | Subscribe 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](/v3/sdks/methods/results).

#### C\#

```csharp
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`.

#### C++

```cpp
void checkSessionNow(authlyx::AuthlyX& app) {
    auto sessionResult = app.checkSession().get();
    std::cout << (sessionResult.ok() ? "Session is active" : sessionResult.message) << '\n';
}

void afterAppWakes(authlyx::AuthlyX& app) {
    auto resumeResult = app.onResume().get();
    std::cout << (resumeResult.ok() ? "Session is active" : resumeResult.message) << '\n';
}
```

Call `checkSessionNow` from your own timer only in manual mode. Call `afterAppWakes` once from your app's resume event. Do not call both for the same wake event. `.get()` waits for the future and returns the result. This is suitable for a console example. In a graphical app, wait on a worker thread, not the UI thread, or the window can freeze while the network request is running.

#### Python

```python
async def check_when_active():
    session_result = await app.checkSession()
    if not session_result.ok:
        print(f"Sign in again: {session_result.message}")

async def after_app_wakes():
    resume_result = await app.onResume()
    if not resume_result.ok:
        print(f"Sign in again: {resume_result.message}")
```

`await` belongs inside `async def`. Call `after_app_wakes()` from your application's resume handler using its asyncio loop.

#### JavaScript / Node.js

```javascript
async function checkWhenActive() {
    const sessionResult = await app.checkSession();
    if (!sessionResult.ok) {
        console.log(`Sign in again: ${sessionResult.message}`);
    }
}

async function afterAppWakes() {
    const resumeResult = await app.onResume();
    if (!resumeResult.ok) {
        console.log(`Sign in again: ${resumeResult.message}`);
    }
}
```

Call `afterAppWakes()` from the event your app uses when it becomes active again.

#### TypeScript

```typescript
async function checkWhenActive(): Promise<void> {
    const sessionResult = await app.checkSession();
    if (!sessionResult.ok) {
        console.log(`Sign in again: ${sessionResult.message}`);
    }
}

async function afterAppWakes(): Promise<void> {
    const resumeResult = await app.onResume();
    if (!resumeResult.ok) {
        console.log(`Sign in again: ${resumeResult.message}`);
    }
}
```

Call `afterAppWakes()` from the event your app uses when it becomes active again.

#### Java

```java
void checkSessionNow(AuthlyX app) {
    app.checkSession().thenAccept(sessionResult -> {
        System.out.println(sessionResult.ok() ? "Session is active" : sessionResult.message());
    });
}

void afterAppWakes(AuthlyX app) {
    app.onResume().thenAccept(resumeResult -> {
        System.out.println(resumeResult.ok() ? "Session is active" : resumeResult.message());
    });
}
```

Call `checkSessionNow` from your own timer only in manual mode. Call `afterAppWakes` once from the framework's resume event. Do not call both for the same wake event. `thenAccept` gives your code the result after the request finishes without making the current thread wait.

#### Go

```go
func checkSession(ctx context.Context, app *authlyx.AuthlyX) error {
    sessionResult, err := app.CheckSession(ctx)
    if err != nil {
        return err
    }
    if sessionResult.Success {
        fmt.Println("Session is active")
    } else {
        fmt.Println("Sign in again: " + sessionResult.Message)
    }
    return nil
}
```

The Go SDK has `CheckSession`, but no separate `OnResume` helper. Call `CheckSession(ctx)` from your app's wake handler. Python and Rust use `onResume`; C#, Java, JavaScript, and TypeScript use `onResume`. C++ uses `onResume` to follow its SDK's current naming style.

#### Rust

```rust
let session_result = app.checkSession().await?;
if session_result.success {
    println!("Session is active");
} else {
    println!("Sign in again: {}", session_result.message);
}
```

`await` waits without blocking the async runtime. `?` returns an SDK/network error from the current async function. A completed response with `success == false` means AuthlyX answered but did not confirm access.

When the app wakes from sleep, call `app.onResume().await?` instead of `app.checkSession().await?`. They perform the same check. Use one call for that moment, not both.

In Rust, the SDK also publishes `Event::SessionInvalidated` when an automatic heartbeat confirms that access ended. Subscribe with `app.subscribe()` if you want to react to that event without manually scheduling checks.

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.