0
0
NestJSframework~10 mins

Local strategy (username/password) 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 Passport local strategy in NestJS.

NestJS
import { [1] } from '@nestjs/passport';
Drag options to blanks, or click blank then click option'
APassportModule
BJwtStrategy
CPassportStrategy
DAuthGuard
Attempts:
3 left
💡 Hint
Common Mistakes
Importing JwtStrategy instead of PassportStrategy
Importing AuthGuard which is not a strategy
Importing PassportModule which is a module, not a strategy
2fill in blank
medium

Complete the constructor to inject the AuthService in the LocalStrategy class.

NestJS
constructor(private readonly [1]: AuthService) {}
Drag options to blanks, or click blank then click option'
AuserService
BpassportService
ClocalService
DauthService
Attempts:
3 left
💡 Hint
Common Mistakes
Using userService which is not injected here
Using localService which is not defined
Using passportService which does not exist
3fill in blank
hard

Fix the error in the validate method signature to properly receive username and password.

NestJS
async validate([1]: string, password: string): Promise<any> {
Drag options to blanks, or click blank then click option'
Aemail
Busername
Cuser
Dlogin
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' which is an object, not a string parameter
Using 'email' which is not the default username field
Using 'login' which is not a standard parameter name
4fill in blank
hard

Fill both blanks to correctly call the AuthService validateUser method and handle invalid user.

NestJS
const user = await this.authService.[1]([2], password);
if (!user) {
  throw new UnauthorizedException();
}
Drag options to blanks, or click blank then click option'
AvalidateUser
Busername
Cemail
DcheckUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'checkUser' which is not defined
Passing 'email' instead of 'username'
Calling a method other than 'validateUser'
5fill in blank
hard

Fill all three blanks to complete the LocalStrategy class extending PassportStrategy and calling super with the correct options.

NestJS
export class LocalStrategy extends PassportStrategy([1]) {
  constructor(private readonly authService: AuthService) {
    super({ usernameField: [2] });
  }

  async validate(username: string, [3]: string): Promise<any> {
    const user = await this.authService.validateUser(username, password);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
Drag options to blanks, or click blank then click option'
AStrategy
B'username'
Cpassword
DLocalStrategy
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'LocalStrategy' instead of 'Strategy' to PassportStrategy
Using usernameField other than 'username'
Missing the password parameter in validate method