> For the complete documentation index, see [llms.txt](https://guilhermesantos.gitbook.io/niro-health/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guilhermesantos.gitbook.io/niro-health/modules/json-web-token.md).

# Json Web Token

The module provides a service that allows you to create and verify JSON Web Tokens.

### 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="/niro-health/modules/configuration.md">Link to Service documentation</a></td></tr><tr><td><code>IValidatorRegexpService</code></td><td><a href="/niro-health/modules/validator-regexp.md">Link to Service documentation</a></td></tr><tr><td><code>IStringExService</code></td><td><a href="/niro-health/modules/stringex.md">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>
