# Json Web Token

### Imports

{% tabs %}
{% tab title="Service" %}

```typescript
import { JsonWebTokenService } from 'niro-health';
```

{% endtab %}

{% tab title="Module" %}

```typescript
import { JsonWebTokenModule } from 'niro-health';
```

{% endtab %}

{% tab title="Interface" %}

```typescript
import type { IJsonWebTokenService } from 'niro-health';
```

{% endtab %}
{% endtabs %}

### Environment Variables

<table><thead><tr><th width="260">name</th><th>description</th></tr></thead><tbody><tr><td><code>JWT_SECRET</code></td><td><code>Secret for Json Web Token.</code></td></tr></tbody></table>

Write **`.env`** file with environment variable. Below has an example:

{% code title=".env" %}

```bash
JWT_SECRET=cLoM/xpKjQpuL825AZexwyitebaOg94Gr4SzBiBNN6M=
```

{% endcode %}

### Method of Use

To use this module, you need to inject it into the desired service.

{% code overflow="wrap" %}

```typescript
import { Module } from '@nestjs/common';
import {
  JsonWebTokenService,
  ConfigurationService,
  ValidatorRegexpService,
  StringExService,
} from 'niro-health';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'IJsonWebTokenService',
      useClass: JsonWebTokenService,
    },
    {
      provide: 'IConfigurationService',
      useClass: ConfigurationService,
    },
    {
      provide: 'IValidatorRegexpService',
      useClass: ValidatorRegexpService,
    },
    {
      provide: 'IStringExService',
      useClass: StringExService,
    },
  ],
})
export class AppModule {}
```

{% endcode %}

Injecting the module into our service.

```typescript
import { Inject, Injectable } from '@nestjs/common';
import type { IJsonWebTokenService } from 'niro-health';

@Injectable()
export class AppService {
  constructor(
    @Inject('IJsonWebTokenService')
    private readonly jsonWebTokenService: IJsonWebTokenService,
  ) {}

  async test() {
    const json = { username: 'GuilhermeSantos001' };
    const token = this.jsonWebTokenService.save(json, 'shhhh', '60s') as string;
    const payload = this.jsonWebTokenService.load(token, 'shhhh');
    return { token, payload };
  }
}
```

### With dependencies

You will need to inject the following interfaces.

<table><thead><tr><th width="308">Interface</th><th>Service</th></tr></thead><tbody><tr><td><code>IConfigurationService</code></td><td><a href="/pages/qVNUf3tpeJXEx7j4GiuW">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/pages/O8NjCtcCgk2uaVu1oyq5">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/pages/9boqQn1bIK4z9uqBM3n0">Link to Service documentation</a></td></tr></tbody></table>

### Methods

<table><thead><tr><th width="301.3333333333333">Method</th><th width="129" align="center">Scope</th><th>Description</th></tr></thead><tbody><tr><td><code>save</code></td><td align="center"><code>public</code></td><td><code>It takes a payload, a secret, and an expiration time, and returns a signed JWT.</code></td></tr><tr><td><code>load</code></td><td align="center"><code>public</code></td><td><code>It takes a token and a secret and returns the payload of the token or an error.</code></td></tr></tbody></table>


---

# 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://guilhermesantos.gitbook.io/niro-health/modules/json-web-token.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.
