# ovok.session

Canonical page: https://docs.ovok.com/web-sdk/core/namespaces/session

`ovok.session` reports who is signed in and keeps the accounts saved on this device. Sign-in itself is in `ovok.auth`. A machine client has no user session: `current` returns `null` and saved accounts throw `OvokConfigError`.

## Read the current session [#read-the-current-session]

```ts
const session = await ovok.session.current();

if (session === null) {
  // signed out
} else {
  console.log(session.profile.reference, session.project.reference);
}
```

- It returns the restored session, or `null` when signed out.
- The `Session` holds the access token, its expiry, the project and the profile.

## Follow session changes [#follow-session-changes]

```ts
const stop = ovok.session.onChange((session) => {
  console.log(session?.profile.reference ?? 'signed out');
});

stop();
```

- The listener fires when a session starts, changes or ends, including when a refused refresh token signs the user out.
- It receives the active `Session` or `null`. The returned function stops listening.

## Switch between saved accounts [#switch-between-saved-accounts]

The client saves a private session for each signed-in profile in the configured storage.

```ts
const accounts = await ovok.session.savedAccounts.list();

await ovok.session.savedAccounts.switch('Patient/123');
await ovok.session.savedAccounts.remove('Patient/123');
```

- `list` returns each account's `id`, `profile`, `project`, `active` and `expired`, without tokens.
- Accounts are identified by profile reference.
- `switch` and `remove` throw `OvokAccountError` when the account is missing or its session has expired (`switch`) or is not saved (`remove`).
- `remove` signs the account out on the server unless it has expired, then forgets it. Removing the active account switches to the next saved one, when there is one.

## Reference [#reference]

- [session API reference](/web-sdk/core/reference/core/session): every method and type.
- [ovok.realtime](/web-sdk/core/namespaces/realtime), which pauses when the session ends.
- [Errors and retries](/web-sdk/core/errors-and-retries)
