> For the complete documentation index, see [llms.txt](https://docs.eximee.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eximee.com/documentation/documentation-en/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/platform-services/pobranie-kontekstu-uzytkownika.md).

# 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.

## User API (api.user.v1)

A universal interface that precisely describes users across all access channels (logged in via Web, via the Dashboard application, in integrated systems, and unauthenticated users).

{% hint style="info" %}
**Availability:** Feature available since platform version **4.737.0**. Works on requests launched in the WebForms application and in the Eximee Dashboard system.
{% endhint %}

### User identity structure (UserIdentity)

{% hint style="warning" %}
**Dependency on implementation:** The structure and contents of the object returned by this method strictly depend on the specifics of the implementation and the authentication mechanisms implemented at a given client. Depending on the platform configuration and the identity provider (IdP), not all of the fields below may be filled in or even available.
{% endhint %}

Method `api.user.v1.getIdentity()` returns an object representing the current identity. The table below describes its default, most commonly encountered properties:

| Field       | Type           | Description                                                                                                                  |
| ----------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `userId`    | String         | Unique user identifier.                                                                                                      |
| `username`  | String         | User name intended for display.                                                                                              |
| `roles`     | Array\<String> | List of roles assigned to the user as an array of strings (e.g. `["EXIMEE_BROKER", "USER"]`).                                |
| `firstName` | String         | User's first name.                                                                                                           |
| `lastName`  | String         | User's last name.                                                                                                            |
| `type`      | String         | Authentication type. Returns one of the values: `'client'` (client), `'employee'` (employee) or `'otp'` (one-time password). |
| `system`    | String         | Identifier of the system providing authentication.                                                                           |

***

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


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

{% hint style="info" %}
The fields username, firstName, lastName, roles depend on the authentication integration method used in the implementation.

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

{% hint style="warning" %}
Since platform version 4.1092.0 the context separates the user identifier from their name:

* `userId` is the user identifier, retrieved from the claim specified in the implementation configuration. Use it wherever you compare or store the user.
* `username` is the user name intended for display. It is not an identifier — it can change and does not have to be unique.
  {% endhint %}

### Retrieving user data

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

### Example of using the User API with role validation

The following script retrieves the user's identity, checks whether they have the appropriate role to perform the action, and if not, throws a business exception and stops execution.

```javascript
function callService(context) {
    const AVAILABLE_ROLES = ["EXIMEE_BROKER_MORTGAGE_DASHBOARD"];
    let identity = api.user.v1.getIdentity();
    let roles = identity.roles;
    
    Logger.info("User identity retrieved. Roles: {}", roles);

    // Verify whether the user has any roles
    if (!roles) {
        throwBusinessError(context, "error_page_default-*", "No permissions to start the process.");
    }
    
    // Check whether the required role is on the user's role list
    let isAvailableRole = roles.some(role => AVAILABLE_ROLES.includes(role.toUpperCase()));

    if (!isAvailableRole) {
        throwBusinessError(context, "error_page_default-*", "No appropriate role to start the process.");
    }
    
    return [{'userId': identity.userId, 'status': 'Authorized'}];

    /**
     * Helper function generating a business error page
     */
    function throwBusinessError(ctx, textcontentName, errorDescription) {
        const builder = ctx.getErrorPageDefinitionBuilder();
        builder.bodyTextContent(textcontentName);
        builder.msg(errorDescription);
        ctx.throwBusinessException(builder);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
