> For the complete documentation index, see [llms.txt](https://developers.portos.sk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.portos.sk/authentication/authentication-schemes/jwt-bearer-authentication.md).

# JWT Bearer Authentication

## API Methods

JWT Token is issued by API after successful login is performed. All subsequent requests have `Authorization` header with value in format `Bearer {tokenId}`. Successful response contains  [`AuthResult`](/data-models.md#authresult) model in response body.

## Authentication

<mark style="color:green;">`POST`</mark> `http://{server-address}/api/auth`

User authorization based on provided credentials. User authenticates to specific device, specified by `DeviceName` property in request body. As result, JWT token is issued.

#### Request Body

| Name                                         | Type   | Description                                            |
| -------------------------------------------- | ------ | ------------------------------------------------------ |
| UserName<mark style="color:red;">\*</mark>   | string | Unique user name.                                      |
| Password                                     | string | User password                                          |
| DeviceName<mark style="color:red;">\*</mark> | string | The unique name of the device the user is logging into |

{% tabs %}
{% tab title="200: OK Authorization successful" %}

```json
{
    "tokenId": "eyJhbG....GLvNNavSk0A",
    "refreshToken": "pC73A....63GkAAEpwtWA=",
    "user": {
        "id": "635f7ae3072edfeae7c26a1e",
        "isVirtual": false,
        "name": "Majiteľ",
        "userName": "999",
        "featureName": null,
        "rights": [
            "admin"
        ],
        "device": {
            "name": "P01",
            "envName": "CashRegister",
            "description": "P01",
            "preferences": {},
            "settings": {
                "MaxQuantity": "200",
                "OpenedPrices": "false",
                "NoticeOnNegativeSale": "false",
                "VisibleQuantities": "false",
                "DefaultFiscalName": "eKasa",
                "DefaultOrderEndpointName": "",
                "DefaultStockName": "S01"
            }
        }
    }
}
```

{% endtab %}

{% tab title="401: Unauthorized Authorization failed" %}

```json
{
    "title": "Nesprávne meno alebo heslo.",
    "status": 401,
    "instance": "/api/auth",
    "errorCode": "Unauthorized",
    "traceId": "0HMM3ODQRAVOG:00000002"
}
```

{% endtab %}
{% endtabs %}

In case of need, you can get user profile associated with `tokenId`, using request below. Successful response contains  [`UserProfileContext`](/data-models.md#userprofilecontext) model in response body.

## Get current user profile

<mark style="color:blue;">`GET`</mark> `http://{server-address}/api/auth`

#### Headers

| Name                                            | Type   | Description                             |
| ----------------------------------------------- | ------ | --------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | String | JWT token in format `Bearer {tokenId}`. |

{% tabs %}
{% tab title="200: OK Successful response" %}

```javascript
{
    "id": "635f7ae3072edfeae7c26a1e",
    "isVirtual": false,
    "name": "Majiteľ",
    "userName": "999",
    "featureName": null,
    "rights": [
        "admin"
    ],
    "device": {
        "name": "P01",
        "envName": "CashRegister",
        "description": "P01",
        "preferences": {},
        "settings": {
            "MaxQuantity": "200",
            "OpenedPrices": "false",
            "NoticeOnNegativeSale": "false",
            "VisibleQuantities": "false",
            "DefaultFiscalName": "eKasa",
            "DefaultOrderEndpointName": "",
            "DefaultStockName": "S01"
        }
    }
}
```

{% endtab %}
{% endtabs %}

Each session has its expiration time (may be modified in API settings). To extends lifespan of session, you can call refresh method. Successful response contains [`RefreshTokenResult`](/data-models.md#refreshtokenresult)  model in response body.

## Refresh session

<mark style="color:green;">`POST`</mark> `http://{server-address}/api/auth/refresh`

Extends session lifespan.

#### Request Body

| Name                                           | Type   | Description                                  |
| ---------------------------------------------- | ------ | -------------------------------------------- |
| tokenId<mark style="color:red;">\*</mark>      | String | Token ID obtained during authorization.      |
| refreshToken<mark style="color:red;">\*</mark> | String | Refresh token obtained during authorization. |

{% tabs %}
{% tab title="200: OK " %}

```javascript
{
    "tokenId": "eyJhbG....GLvNNavSk0A",
    "refreshToken": "pC73A....63GkAAEpwtWA="
}
```

{% endtab %}
{% endtabs %}

To sign out and terminate user session, send DELETE request.

## Terminate session

<mark style="color:red;">`DELETE`</mark> `http://{server-address}/api/auth`

Logs out user.

#### Headers

| Name                                            | Type   | Description                             |
| ----------------------------------------------- | ------ | --------------------------------------- |
| Authorization<mark style="color:red;">\*</mark> | string | JWT token in format `Bearer {tokenId}`. |

{% tabs %}
{% tab title="200: OK Always returns OK with empty body." %}

```javascript
{
    // Response
}
```

{% endtab %}
{% endtabs %}


---

# 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://developers.portos.sk/authentication/authentication-schemes/jwt-bearer-authentication.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.
