Questions tagged [nestjs-jwt]

This package provides JSON Web Token (JWT) utilities and helps create tokens for use in authentication and authorization of web requests to your NestJS application. It contains a JwtModule that exposes a single JwtService provider to sign, verify and decode JWT tokens, either synchronously or asynchronously. The JwtService is configured with options (via JwtModuleOptions) that correspond to config of the NodeJS jsonwebtoken package used underneath.

Provides JWT utilities module for NestJS based on the jsonwebtoken package.

From the documentation:

The @nestjs/jwt package is a utility package that helps with JWT manipulation. The passport-jwt package is the Passport package that implements the JWT strategy and @types/passport-jwt provides the TypeScript type definitions.

From the project README.md:

Installation

$ npm i --save @nestjs/jwt

Usage

Import JwtModule:

@Module({
 imports: [JwtModule.register({ secret: 'hard!to-guess_secret' })],
 providers: [...],
})
export class AuthModule {}

Inject JwtService:

@Injectable()
export class AuthService {
 constructor(private readonly jwtService: JwtService) {}
}

More information

77 questions
16
votes
4 answers

NestJs Passport jwt unknown strategy

I am trying to implement a JWT strategy for authentication in my nest application. I am getting the following error tho Unknown authentication strategy "jwt" This is my code: jwt.strategy.ts import { Injectable } from "@nestjs/common"; import {…
Jan Krüger
  • 786
  • 1
  • 5
  • 16
11
votes
3 answers

Authorization in Nestjs using graphql

I have started to learn Nestjs, express and graphql. I encountered a problem while trying to authorize access of user authenticated using jwt token. I followed the tutorial for authentication on the Nestjs website. I am able to get the current user,…
Adriel
  • 183
  • 1
  • 2
  • 11
10
votes
4 answers

How to implement multiple JWT strategies using multiple secrets in Nest.JS

I like to implement more than one named passport-JWT strategy, each with its own secret. Is there any way it can be implemented? From what I can understand from the documentation, only one secret can be registered during module…
Arnab De
  • 402
  • 4
  • 12
9
votes
2 answers

@nestjs/swagger does not set authorization headers

Can't authorize in a route using @nestjs/swagger@5.0.9 because I dont know how to configure the Document` in a right way and I couldn't find a workable answer in authorization official docs / stackoverflow / github. I've stumbled upon a problem with…
wald3
  • 343
  • 1
  • 3
  • 13
9
votes
1 answer

PassportJS, NestJS: canActivate method of AuthGuard('jwt')

Does anybody know where I can see the full code of canActivate method in AuthGuard('jwt')? I realized that canActivate method calls JwtStrategy validate method by using console.log() like this: // jwt.strategy.ts @Injectable() export class…
9
votes
3 answers

Nest can't resolve dependencies of AuthService

I am following the documentation present here https://docs.nestjs.com/techniques/authentication#jwt-functionality To have faster support I have created a git repository with the problem https://github.com/Sano123456/nestjs-jwt node -v ->…
Sano
  • 469
  • 2
  • 6
  • 21
7
votes
3 answers

How can I user configService in super()?

I have a question about setting environmental variables. In official document, it says using ConfigModule in this case, but my case is a exception case. Because I would like to use it in super() in constructor. My code is the below. Is there any…
shun
  • 71
  • 1
  • 2
7
votes
4 answers

Cannot find module 'passport' or its corresponding type declarations

I am using @nestjs/passport. After running npm run start:dev I got an error, but the editor doesn't show an error node_modules/@nestjs/passport/dist/passport/passport.serializer.d.ts:1:27 - error TS2307: Cannot find module 'passport' or its…
PrabodhanaJ
  • 263
  • 1
  • 4
  • 12
6
votes
6 answers

NestJs JWT Authentication returns 401

I have implemented a jwt authentication in nestJs. However whenever I attempt to authenticate using the following authorization headers: Bearer or JWT I got 401. These are my authentication files @Injectable() export class…
Arsene
  • 1,037
  • 4
  • 20
  • 47
6
votes
7 answers

NestJS jwt-passport Authentication

I want to implement a distributed authentication library to use it on several projects. The library should implement JWT authentication method. The code is as follows: jwt.strategy.ts import {ExtractJwt, Strategy} from 'passport-jwt'; import…
CountZero
  • 186
  • 1
  • 1
  • 8
5
votes
3 answers

NestJS/TypeORM unit testing: Can't resolve dependencies of JwtService

I'm trying to unit test this controller and mock away the services/repositories that it needs. @Controller('auth') export class AuthController { constructor( private readonly authService: AuthService, private readonly…
noblerare
  • 10,277
  • 23
  • 78
  • 140
4
votes
5 answers

NestJS and passport-local : AuthGuard('local') validate() never get called

I've followed the official Nest doc (https://docs.nestjs.com/security/authentication) step by step, but I can't get validate() method called when using @AuthGuard('local') or @AuthGuard(LocalAuthGuard) on login action. If I don't use that guard…
Gigs
  • 199
  • 1
  • 12
4
votes
3 answers

UnauthorizedException is deliver as Internal Server Error

I'm trying a create a shared Guard as an external library in order to be imported and used across services. I'm not doing anything special that what is described in some guides but with the particularity that the code will reside in a shared…
maturanomx
  • 88
  • 1
  • 9
4
votes
2 answers

Is there a native method in nestjs to decode JWT?

In nestjs I create JWT (tokens) by creating a payload object and signing it. Something like this: const jwtPayload: JwtPayload = { iss: issuer, sub: info, aud: audience, //…
Felix
  • 1,662
  • 2
  • 18
  • 37
4
votes
1 answer

NestJS - Use service inside Interceptor (not global interceptor)

I have a controller that uses custom interceptor: Controller: @UseInterceptors(SignInterceptor) @Get('users') async findOne(@Query() getUserDto: GetUser) { return await this.userService.findByUsername(getUserDto.username) } I…
Amiga500
  • 5,874
  • 10
  • 64
  • 117
1
2 3 4 5 6