0
0
NestJSframework~10 mins

JWT authentication guard 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 guard from NestJS.

NestJS
import { [1] } from '@nestjs/passport';
Drag options to blanks, or click blank then click option'
AAuthGuard
BJwtGuard
CJwtAuthGuard
DJwtStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Importing JwtAuthGuard directly (it doesn't exist by default).
Importing JwtStrategy instead of AuthGuard.
2fill in blank
medium

Complete the code to create a JWT authentication guard class.

NestJS
export class JwtAuthGuard extends [1]('jwt') {}
Drag options to blanks, or click blank then click option'
AJwtStrategy
BJwtGuard
CPassportGuard
DAuthGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Using JwtGuard which does not exist.
Extending JwtStrategy instead of AuthGuard.
3fill in blank
hard

Fix the error in the guard usage in a controller method decorator.

NestJS
@UseGuards([1])
getProfile() {
  return 'profile data';
}
Drag options to blanks, or click blank then click option'
AJwtAuthGuard()
BAuthGuard('jwt')
CJwtAuthGuard
DJwtGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses after the guard class name.
Using a non-existent JwtGuard.
4fill in blank
hard

Fill both blanks to implement a custom canActivate method that logs before calling the base guard.

NestJS
async canActivate(context: ExecutionContext): Promise<boolean> {
  console.log([1]);
  return (await super.canActivate([2])) as boolean;
}
Drag options to blanks, or click blank then click option'
A'Checking JWT guard'
Bcontext
Cctx
D'JWT check started'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable named ctx instead of context.
Logging a variable instead of a string message.
5fill in blank
hard

Fill all three blanks to extract the user from the request inside the guard.

NestJS
const request = context.switchToHttp().get[1]();
const user = request.[2];
return user !== [3];
Drag options to blanks, or click blank then click option'
ARequest
Brequest
Cnull
Duser
Ereq
Fbody
Gundefined
Hparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using getBody() or getParams() instead of getRequest().
Checking user against undefined instead of null.