0
0
NestJSframework~30 mins

Passport.js integration in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Passport.js Integration in NestJS
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Create a NestJS module named AuthModule that imports PassportModule with the local strategy, provides LocalStrategy, and exports PassportModule.