Bird
0
0

Identify the error in this LocalStrategy validate method:

medium📝 Debug Q14 of 15
NestJS - Authentication
Identify the error in this LocalStrategy validate method:
async validate(username: string, password: string) {
  const user = this.authService.validateUser(username, password);
  if (!user) {
    throw new UnauthorizedException();
  }
  return user;
}
AMissing await before authService.validateUser call
BUnauthorizedException should not be thrown
Cvalidate method should not be async
Dusername and password parameters are reversed
Step-by-Step Solution
Solution:
  1. Step 1: Check async call usage

    The method calls validateUser which returns a Promise, but lacks await.
  2. Step 2: Understand consequences of missing await

    Without await, user is a Promise, so if (!user) check is incorrect and login logic breaks.
  3. Final Answer:

    Missing await before authService.validateUser call -> Option A
  4. Quick Check:

    Async calls need await to get results [OK]
Quick Trick: Always await async calls in validate() [OK]
Common Mistakes:
  • Forgetting await on async service calls
  • Throwing exceptions incorrectly
  • Misordering parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes