0
0
NestJSframework~10 mins

Token generation and validation in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the JWT module in a NestJS service.

NestJS
import { [1] } from '@nestjs/jwt';
Drag options to blanks, or click blank then click option'
AJwtStrategy
BJwtModule
CJwtGuard
DJwtService
Attempts:
3 left
💡 Hint
Common Mistakes
Importing JwtModule instead of JwtService
Using JwtGuard which is for authorization
Using JwtStrategy which is for passport strategies
2fill in blank
medium

Complete the code to generate a JWT token with a payload.

NestJS
const token = this.jwtService.[1](payload);
Drag options to blanks, or click blank then click option'
AsignAsync
Bsign
Cdecode
Dverify
Attempts:
3 left
💡 Hint
Common Mistakes
Using verify or decode instead of sign
Using signAsync when synchronous sign is intended
3fill in blank
hard

Fix the error in the code to verify a JWT token.

NestJS
const payload = this.jwtService.[1](token);
Drag options to blanks, or click blank then click option'
Adecode
Bsign
Cverify
DsignAsync
Attempts:
3 left
💡 Hint
Common Mistakes
Using decode which does not validate
Using sign which creates tokens
Using signAsync which is for signing
4fill in blank
hard

Fill both blanks to create a JWT token with expiration and secret options.

NestJS
const token = this.jwtService.sign(payload, { [1]: '1h', [2]: 'mySecretKey' });
Drag options to blanks, or click blank then click option'
AexpiresIn
Bsecret
Calgorithm
Daudience
Attempts:
3 left
💡 Hint
Common Mistakes
Using algorithm or audience instead of expiresIn or secret
Mixing up option names
5fill in blank
hard

Fill all three blanks to decode a token safely with error handling.

NestJS
try {
  const payload = this.jwtService.[1](token, { [2]: 'mySecretKey' });
  return payload;
} catch (error) {
  throw new [3]('Invalid token');
}
Drag options to blanks, or click blank then click option'
Averify
Bsecret
CUnauthorizedException
Ddecode
Attempts:
3 left
💡 Hint
Common Mistakes
Using decode which does not validate
Not providing the secret key
Throwing generic errors instead of UnauthorizedException