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 {}
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.
fixAdd 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.
fixEnsure 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.
fixImplement verify(params: any): Promise<UserInterface | undefined> in your service.
Audit
Dependencies
@nestjs/commonoptionalPeer dependency: NestJS decorators and guard base class
@nestjs/coreoptionalPeer dependency: NestJS runtime and metadata handling