Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing JwtModule instead of JwtService
Using JwtGuard which is for authorization
Using JwtStrategy which is for passport strategies
✗ Incorrect
The JwtService is imported from @nestjs/jwt to generate and validate tokens.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using verify or decode instead of sign
Using signAsync when synchronous sign is intended
✗ Incorrect
The sign method creates a JWT token synchronously from the payload.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decode which does not validate
Using sign which creates tokens
Using signAsync which is for signing
✗ Incorrect
The verify method checks the token's validity and returns the decoded payload.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using algorithm or audience instead of expiresIn or secret
Mixing up option names
✗ Incorrect
The expiresIn option sets token expiry time, and secret defines the signing key.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decode which does not validate
Not providing the secret key
Throwing generic errors instead of UnauthorizedException
✗ Incorrect
Use verify to check the token with the secret. If invalid, throw UnauthorizedException to signal access denial.