Registry / security / nestjs-guard-grpc

nestjs-guard-grpc

JSON →
library0.1.3jsnpmunverified

GrpcAuthGuard is an agnostic guard for NestJS optimized for gRPC scope, allowing injection of a custom authentication service that implements the IAuthService interface. It reads tokens from gRPC metadata and provides a @GRPCUser() decorator to inject the authenticated user into controller methods. Current version is 0.1.3 (beta). The library is minimal and tightly coupled to NestJS's gRPC microservices, offering a straightforward pattern for token validation (e.g., JWT) without opinionated defaults. Compared to other NestJS guards, this one is purpose-built for gRPC and metadata-based auth.

npm install nestjs-guard-grpc
INSTALL
IMPORT
SIG · NESTJS-GUARD-GRPC
N
nestjs-guard-grpc
securityjavascriptv0.1.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GrpcAuthGuard
import { GrpcAuthGuard } from 'nestjs-guard-grpc'
const GrpcAuthGuard = require('nestjs-guard-grpc')
ESM package: use import syntax.
GRPCUser
import { GRPCUser } from 'nestjs-guard-grpc'
import { GrpcUser } from 'nestjs-guard-grpc'
Case-sensitive: capitalize 'GRPC'.
IAuthService
import { IAuthService } from 'nestjs-guard-grpc'
import { AuthService } from 'nestjs-guard-grpc'
Interface name is 'IAuthService', not 'AuthService'.
UserInterface
import { UserInterface } from 'nestjs-guard-grpc'
import { User } from 'nestjs-guard-grpc'
The type is named 'UserInterface', not 'User'.

Shows how to create a custom auth service implementing IAuthService, use GrpcAuthGuard with @UseGuards, and inject the authenticated user via @GRPCUser decorator in a NestJS gRPC controller.

import { Controller, UseGuards } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; import { GrpcAuthGuard, GRPCUser, IAuthService, UserInterface } from 'nestjs-guard-grpc'; import { Injectable } from '@nestjs/common'; import * as jwt from 'jsonwebtoken'; import { UserController as Ctrl, User } from './user.interface'; @Injectable() export class MyAuthService implements IAuthService { async verify(params: any): Promise<UserInterface | undefined> { const token = params; return new Promise((resolve, reject) => { jwt.verify(token, 'secret', {}, (err, decoded) => { if (err) reject(err); resolve(decoded as UserInterface); }); }); } } @Controller() export class UserController { @UseGuards(GrpcAuthGuard) @GrpcMethod('UserService', 'FindAll') findAll(@GRPCUser() user: UserInterface, metadata: any) { console.log('User injected', user); return []; } } // Module registration @Module({ controllers: [UserController], providers: [ MyAuthService, { provide: 'IAuthService', useClass: MyAuthService }, ], }) export class UsersModule {}
Debug
Known issues
gotchaThe token must be passed in gRPC metadata under an 'Authorization' header with 'Bearer ' prefix.
fix
Ensure client sends metadata like { Authorization: 'Bearer ' + jwt }.
affects: >=0.1.0
gotchaIAuthService must be provided with the token 'IAuthService' as a custom provider in the module.
fix
Add { provide: 'IAuthService', useClass: YourService } to module providers.
affects: >=0.1.0
deprecatedPackage version 0.1.x is in early development; interfaces or token names may change without major version bump.
fix
Pin exact version or monitor releases for breaking changes.
affects: <1.0.0
gotchaGrpcAuthGuard reads metadata from gRPC call context; it does not work with HTTP or other transports.
fix
Only use with NestJS gRPC microservices (use @nestjs/microservices).
affects: >=0.1.0
Errors
Common errors & fixes
Nest can't resolve dependencies of GrpcAuthGuard (?). Please make sure that the argument IAuthService at index [0] is available in the current context.
Missing provider for IAuthService token in the module where GrpcAuthGuard is used.
fix
Add a provider with 'IAuthService' token: { provide: 'IAuthService', useClass: MyAuthService }
Cannot read properties of undefined (reading 'getMap')
GrpcAuthGuard is used in a non-gRPC context (e.g., HTTP controller) where metadata is absent.
fix
Ensure the guard is only applied to gRPC methods (decorated with @GrpcMethod).
IAuthService.verify is not a function
The custom auth service does not implement the verify method correctly.
fix
Implement verify(params: any): Promise<UserInterface | undefined> in your service.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies
@nestjs/commonoptionalPeer dependency: NestJS decorators and guard base class
@nestjs/coreoptionalPeer dependency: NestJS runtime and metadata handling
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources
nestjs-guard-grpc — npm install nestjs-guard-grpc · libregistry