📖 Scenario: You are building a simple NestJS application that needs user authentication. You will integrate Passport.js to handle user login using a local strategy.
🎯 Goal: Build a NestJS module that integrates Passport.js with a local strategy for user authentication. You will create the user data, configure Passport, implement the authentication logic, and complete the module setup.
📋 What You'll Learn
Create a user data array with exact users
Add a configuration variable for the local strategy name
Implement Passport local strategy using the user data
Complete the AuthModule with Passport integration
💡 Why This Matters
🌍 Real World
User authentication is essential for most web applications. Passport.js is a popular library to handle authentication strategies in Node.js frameworks like NestJS.
💼 Career
Understanding Passport.js integration in NestJS is valuable for backend developers working on secure user login systems.
Progress0 / 4 steps
1
Create user data array
Create a constant array called users with these exact user objects: { userId: 1, username: 'john', password: 'changeme' } and { userId: 2, username: 'maria', password: 'guess' }.
NestJS
Hint
Define a constant array named users with two user objects exactly as shown.
2
Add local strategy name configuration
Create a constant string called LOCAL_STRATEGY_NAME and set it to 'local'.
NestJS
Hint
Define a constant string LOCAL_STRATEGY_NAME with the value 'local'.
3
Implement Passport local strategy
Create a class called LocalStrategy that extends PassportStrategy with Strategy from passport-local. Implement a validate method that takes username and password, finds the matching user in users, and returns the user if the password matches. If no user or wrong password, throw UnauthorizedException.
NestJS
Hint
Extend PassportStrategy with Strategy and implement validate to check username and password against users.
4
Complete AuthModule with Passport integration
Create a NestJS module class called AuthModule that imports PassportModule with defaultStrategy set to LOCAL_STRATEGY_NAME. Add LocalStrategy to the providers array and export PassportModule.
NestJS
Hint
Create a NestJS module named AuthModule that imports PassportModule with the local strategy, provides LocalStrategy, and exports PassportModule.
Practice
(1/5)
1. What is the main purpose of integrating Passport.js in a NestJS application?
easy
A. To handle user authentication and login strategies
B. To manage database connections
C. To style the frontend components
D. To optimize server performance
Solution
Step 1: Understand Passport.js role
Passport.js is a library designed to handle user authentication in Node.js applications.
Step 2: Connect Passport.js with NestJS
In NestJS, Passport.js is used to define login strategies and verify users securely.
Final Answer:
To handle user authentication and login strategies -> Option A
Quick Check:
Passport.js = Authentication [OK]
Hint: Passport.js is for login and user verification [OK]
Common Mistakes:
Confusing Passport.js with database tools
Thinking Passport.js styles UI
Assuming Passport.js improves server speed
2. Which of the following is the correct way to apply a Passport.js local strategy in a NestJS controller?
easy
A. @UseGuards('local')
B. @UseGuards(LocalStrategy)
C. @UseGuards(AuthGuard('local'))
D. @UseGuards(PassportLocal)
Solution
Step 1: Recall NestJS guard syntax
Passport strategies are applied using AuthGuard with the strategy name as a string.
Step 2: Match correct syntax
The correct syntax is @UseGuards(AuthGuard('local')) to apply the local strategy guard.
Final Answer:
@UseGuards(AuthGuard('local')) -> Option C
Quick Check:
UseGuards with AuthGuard('strategy') [OK]
Hint: Use AuthGuard with strategy name string [OK]
Common Mistakes:
Passing strategy class instead of string
Omitting AuthGuard wrapper
Using incorrect decorator syntax
3. Given this NestJS Passport.js setup, what will be the output when a user with correct credentials logs in?
A. AuthGuard should receive strategy name as string, not class
B. Missing @Controller decorator
C. getProfile method must be async
D. AuthGuard cannot be used with @Get
Solution
Step 1: Check AuthGuard parameter
AuthGuard expects a string with the strategy name, e.g., 'local', not the strategy class.
Step 2: Confirm correct usage
Correct usage is @UseGuards(AuthGuard('local')), passing the strategy name as a string.
Final Answer:
AuthGuard should receive strategy name as string, not class -> Option A
Quick Check:
AuthGuard('strategyName') not class [OK]
Hint: Pass strategy name string to AuthGuard [OK]
Common Mistakes:
Passing strategy class instead of string
Forgetting to import AuthGuard
Assuming method must be async
5. You want to protect a NestJS route so only logged-in users can access it using Passport.js. Which combination correctly sets this up?
1. Create a JWT strategy class
2. Use @UseGuards(AuthGuard('jwt')) on the route
3. Implement validate() method in strategy
4. Call AuthGuard('local') on the route
Which steps should you follow?
hard
A. All steps 1, 2, 3, and 4
B. Steps 1, 2, and 3 only
C. Steps 1 and 4 only
D. Steps 2 and 4 only
Solution
Step 1: Understand JWT strategy usage
To protect routes with JWT, create a JWT strategy and implement validate() to verify tokens.
Step 2: Apply AuthGuard with 'jwt'
Use @UseGuards(AuthGuard('jwt')) on routes to enforce JWT authentication.
Step 3: Exclude local strategy for JWT protection
Using AuthGuard('local') is for login, not for protecting routes after login.
Final Answer:
Steps 1, 2, and 3 only -> Option B
Quick Check:
JWT strategy + AuthGuard('jwt') + validate() [OK]
Hint: Use JWT strategy and AuthGuard('jwt') to protect routes [OK]
Common Mistakes:
Using local strategy guard to protect routes
Skipping validate() method in strategy
Applying AuthGuard without strategy name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.