@txdmobile/nestjs-auth
v7.0.0
Published
Nestjs library for auth using private and public key with user roles
Readme
TxD Mobile NestJS Auth
Nestjs library for auth using private and public key with user roles
Installation
$ npm install --save @nestjs/passport passport
$ npm install --save @txdmobile/nestjs-authImport Module
import { NestjsAuthModule } from '@txdmobile/nestjs-auth';
const config = {
publicKey: "<PUBLIC_KEY>"
}
@Module({
imports: [NestjsAuthModule.config(config)],
...
Note: NestjsAuthModule was defined with the decorator "@Global", therefore it should only be imported in the root module of the app
Use Bearer Authentication
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { RolesGuard, Roles } from '@txdmobile/nestjs-auth';
import { AuthGuard } from '@nestjs/passport';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
@UseGuards(AuthGuard('bearer'))
getHello(): string {
return this.appService.getHello();
}
}Use Bearer Authentication with Roles
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { RolesGuard, Roles } from '@txdmobile/nestjs-auth';
import { AuthGuard } from '@nestjs/passport';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
@Roles('admin')
@UseGuards(AuthGuard(), RolesGuard)
getHello(): string {
return this.appService.getHello();
}
}Retrieve the user object from request
...
@Get('user')
@UseGuards(AuthGuard())
getUser(@Req() req) {
return req.user;
}
//this code returns the following json
{
"uid": "<username>",
"roles": [
"admin"
]
}Publicar librería
cada vez que se desee publicar una actualizacion de version se debe incrementar la version en package.json
{
"name": "@txdmobile/nestjs-auth",
"version": "6.X.X", // actualizar valor
...
}Debe seguir la siguiente nomenclatura:
7.0.X: bugs y fixs de seguridad7.X.0: nueva funcionalidadX.0.0: breaking change version
Se debe tener la siguiente variable de ambiente definida NPM_TOKEN, el cual debe ser un token de tipo automatation el cual servira para publicar un paquete de npm sin necesidad de 2F
#!/bin/bash
export NPM_TOKEN="npm_XXXXX"
npm run lib:publish # this run npm `run build` and `npm publish`
