> 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/budowanie-aplikacji/logika-biznesowa/scriptcode/skrypty-scriptservice/api-skryptow/pobranie-kontekstu-uzytkownika.md).

# Pobieranie kontekstu zalogowanego użytkownika

API pozwala na pobranie kontekstu użytkownika aktualnie wypełniającego formularz. Kontekst opisuje użytkowników zalogowanych na formularzu, w aplikacji Dashboard czy aplikacjach klienta zintegrowanych z systemem Eximee oraz informacje o sesji użytkownika niezalogowanego.

{% hint style="warning" %}
Funkcjonalność dostępna od wersji platformy: 4.737.0

Funkcjonalność działa na wnioskach uruchamianych w aplikacji webforms.
{% 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" %}
Pola: firstName, lastName, roles są zależne od sposobu integracji uwierzytelniania w ramach wdrożenia.

Najczęściej pola są dostępne jedynie dla uwierzytelnienia typu Pracownik (employee) w systemach Dashboard lub systemach Klienta zintegrowanych z Eximee.
{% endhint %}

#### Kontekst działania

API jest dostępne z obiektu `api.user.v1` i działa w kontekście formularza.

Obsługuje użytkowników pracujących z formularzem:

* zalogowanych przez kanał Web zgodnie z integracją wdrożenia,
* zalogowanych przez aplikację Dashboard,
* zalogowanych przez autoryzację zintegrowanych systemów wdrożenia,
* niezalogowanych w podstawowym zakresie rozróżnialności użytkowników.

***

## Przykłady użycia

### Pobieranie danych użytkownika

```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,
    }];
} 
```

### Przykład użycia w skrypcie

```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-*", "Brak uprawnien do uruchomienia procesu")
    }
    let isAvailableRole = roles.some(role => AVAILABLE_ROLES.includes(role.toUpperCase()))

    if (!isAvailableRole) {
        throwBusinessError("error_page_default-*", "Brak odpowiedniej roli do uruchomienia procesu")

    }
    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
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 dynamically by asking a question.

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

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
