Bird
0
0

Examine this LocalStrategy validate method:

medium📝 Debug Q6 of 15
NestJS - Authentication
Examine this LocalStrategy validate method:
async validate(username: string, password: string) {
  const user = await this.authService.validateUser(username, password);
  if (!user) {
    return null;
  }
  return user;
}

What is the main issue with this implementation?
AUsing await with validateUser which should be synchronous
BReturning null instead of throwing an UnauthorizedException when user is not found
CNot hashing the password before validation
DReturning the user object directly without removing sensitive data
Step-by-Step Solution
Solution:
  1. Step 1: Understand the validate method's role

    The validate method must reject invalid credentials by throwing an UnauthorizedException.
  2. Step 2: Analyze the current behavior

    Returning null when the user is not found will not trigger the proper authentication failure response.
  3. Final Answer:

    Returning null instead of throwing an UnauthorizedException when user is not found -> Option B
  4. Quick Check:

    Authentication failure must throw UnauthorizedException [OK]
Quick Trick: Always throw UnauthorizedException on invalid credentials [OK]
Common Mistakes:
  • Returning null instead of throwing an exception
  • Not awaiting asynchronous calls
  • Not handling invalid credentials properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes