# Retrieving the logged-in user context

The API allows retrieving the context of the user currently filling out the form. The context describes users logged in on the form, in the Dashboard application, or in client applications integrated with the Eximee system, as well as information about the session of a user who is not logged in.

{% hint style="warning" %}
Functionality available since platform version: 4.737.0

The functionality works on applications run in the webforms application.
{% endhint %}

***

## API

```typescript
interface User {
    v1: {
        getIdentity(): UserIdentity
    }
}


interface UserIdentity {
    userId(): string,
    roles(): string[],
    firstName(): string,
    lastName(): string,
    type(): 'client'|'employee'|'otp',
    system(): string
}
```

{% hint style="info" %}
Fields: firstName, lastName, roles depend on the authentication integration method within the implementation.

Most often, the fields are available only for Employee-type authentication (employee) in Dashboard systems or Client systems integrated with Eximee.
{% endhint %}

#### Operation context

The API is available from the object `api.user.v1` and works in the form context.

It supports users working with the form:

* logged in through the Web channel according to the implementation integration,
* logged in through the Dashboard application,
* logged in through authorization of integrated implementation systems,
* not logged in, within the basic scope of user distinguishability.

***

## Usage examples

### Retrieving user data

```typescript
function callService(context) {
    const identity = api.user.v1.getIdentity();
    Logger.info("Get user identity: {}", identity)
    return [{
        'userId': identity.userId,
        'firstName': identity.firstName,
        'lastName': identity.lastName,
        'type': identity.type,
        'system': identity.system,
    }];
} 
```

### Example of use in a script

```typescript
function callService(context) {
    const AVAILABLE_ROLES = ["EXIMEE_BROKER_MORTGAGE_DASHBOARD"]
    let identity = api.user.v1.getIdentity()
    let roles = identity.roles
    Logger.info("simpleMortgageExecutionPermissionVerify roles: {}", roles)

    if (!roles) {
        throwBusinessError("error_page_default-*", "No permission to start the process")
    }
    let isAvailableRole = roles.some(role => AVAILABLE_ROLES.includes(role.toUpperCase()))

    if (!isAvailableRole) {
        throwBusinessError("error_page_default-*", "No appropriate role to start the process")

    }
    let userId = identity.userId
    return [{ userId }];

    function throwBusinessError(textcontentName, errorDescription) {
        const builder = context.getErrorPageDefinitionBuilder();
        builder.bodyTextContent(textcontentName);
        builder.msg(errorDescription);
        context.throwBusinessException(builder);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.eximee.com/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/pobranie-kontekstu-uzytkownika.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
