📝Json Web Token
The module provides a service that allows you to create and verify JSON Web Tokens.
Imports
import { JsonWebTokenService } from 'niro-health';
Environment Variables
name
description
JWT_SECRET
Secret for Json Web Token.
Write .env
file with environment variable. Below has an example:
JWT_SECRET=cLoM/xpKjQpuL825AZexwyitebaOg94Gr4SzBiBNN6M=
Method of Use
To use this module, you need to inject it into the desired service.
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 {}
Injecting the module into our service.
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.
Interface
Service
IConfigurationService
IValidatorRegexpService
IStringExService
Methods
Method
Scope
Description
save
public
It takes a payload, a secret, and an expiration time, and returns a signed JWT.
load
public
It takes a token and a secret and returns the payload of the token or an error.
Last updated