openmrs-esm-core

O3 Framework / getCurrentUser

Function: getCurrentUser()

Call Signature

getCurrentUser(): Promise<Session>

Defined in: packages/framework/esm-api/src/current-user.ts:105

The getCurrentUser function returns a Promise that resolves once with the current user’s session. If the session hasn’t been loaded, was loaded more than a minute ago, or is in the middle of being refetched, the Promise waits for the fetch in question rather than resolving with data that may be out of date. The session it resolves with is therefore never more than a minute old, unless that fetch fails, in which case it resolves with the last session that loaded.

The function accepts an optional opts object with an includeAuthStatus boolean property that defaults to true. When true, the entire Session object from the API is provided. When false, only the LoggedInUser property of the response is provided.

To react to subsequent session changes (login, logout, user-property updates), use getSessionStore (getState() / subscribe()) or the useSession React hook rather than calling this repeatedly.

Returns

Promise<Session>

A Promise resolving to a LoggedInUser object (if includeAuthStatus is false) or a Session object (if includeAuthStatus is true or not provided).

Example

import { getCurrentUser } from '@openmrs/esm-api'
const session = await getCurrentUser({ includeAuthStatus: true })
console.log(session.authenticated)

Call Signature

getCurrentUser(opts): Promise<Session>

Defined in: packages/framework/esm-api/src/current-user.ts:112

Parameters

opts

Options for controlling the response format.

includeAuthStatus

true

When true, resolves with the full Session object including authentication status.

Returns

Promise<Session>

A Promise resolving to a Session object.

Call Signature

getCurrentUser(opts): Promise<LoggedInUser>

Defined in: packages/framework/esm-api/src/current-user.ts:119

Parameters

opts

Options for controlling the response format.

includeAuthStatus

false

When false, resolves with only the LoggedInUser object without the surrounding session information.

Returns

Promise<LoggedInUser>

A Promise resolving to a LoggedInUser object.