> For the complete documentation index, see [llms.txt](https://docs.mobguards.com/ru/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mobguards.com/ru/backend-integration/jwt-token/node-js.md).

# NodeJS

### Требования

Чтобы иметь возможность декодировать токен JWT с помощью языка JavaScript, необходимо установить пакет `jsonwebtoken`.

Лучший способ установить его - через менеджер пакетов `npm`:

```bash
$ npm install jsonwebtoken
```

### Использование jsonwebtoken

Во-первых, необходимо импортировать пакет jsonwebtoken:

```javascript
const jwt = require('jsonwebtoken');
```

Во-вторых, необходимо прочитать и сохранить публичный ключ (используемый для расшифровки токена). Нужно заранее получить его заранее и хранить рядом с проектом (в данном случае `./keys/id_rsa.pub`).

```javascript
const PUBLIC_KEY = fs.readFileSync('/path/to/public/key_public.pem'); // get public key
```

Затем необходимо реализовать функцию для проверки токена с использованием публичного ключа:

```javascript
function verifyJwt(token) {
    try {
        return jwt.verify(token, PUBLIC_KEY, { algorithms: ['RS256'] });
    }
    catch(e) {
        return;
    }
}
```

```javascript
result = verifyJwt(token);
```

### Результат

Если токен правильный и срок его действия не истек, после использования `jwt_verify` будет получен декодированную структуру JWT:

```javascript
{
  "challenge_id": "3aa67786-d8cb-4ed5-ae83-ae7ace27e372",
  "result": 94,
  "iat": 1578001068,
  "exp": 1578001128
}
```

Результат проверки бот/не бот можно получить из поля `result`.


---

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

```
GET https://docs.mobguards.com/ru/backend-integration/jwt-token/node-js.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.
