This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install validateroutemslib
Import library Module, with 'url' in initialize, and Guards your module project.
import { Global, Module } from '@nestjs/common';
import { ValidateService } from 'application/use-cases/validate/validate.service';
import { ValidateRouteMsLibModule, EvaAuthGuard, EvaRoleGuard } from 'validateroutemslib';
import { APP_GUARD } from '@nestjs/core';
import * as dotenv from 'dotenv';
dotenv.config();
@Module({
imports: [ValidateRouteMsLibModule.initialize('url')],
providers: [
{
provide: APP_GUARD,
useClass: EvaAuthGuard,
},
{
provide: APP_GUARD,
useClass: EvaRoleGuard,
},
]
})
export class ValidateModule {}
Create file with decorator.
roles.decorator
import { SetMetadata } from '@nestjs/common';
export const Roles = (...roles: string[]) => SetMetadata('roles', roles);
Import '@UseGuards()' guards decorator to validate token. If you want includes privileges add @Roles('ROLES')
import { UseGuards, Get, Param } from '@nestjs/common';
import { Roles } from '@/shared/decorators/roles/roles.decorator';
import { User } from '@shared/decorators/user/user.decorator';
import { EvaRole } from 'validateroutemslib';
@Get(':postId')
@Roles(EvaRole.Super) // optional
@UseGuards()
async getPostById(
@User() user,
@Param('postId') postId: number,
): Promise<any> {
return this.postService.getPostById(user, postId);
}