Performance: Passport.js integration
This affects the server response time and initial page load speed by adding authentication middleware processing.
app.use(passport.initialize()); // Use session only if needed // Load strategies lazily or conditionally passport.use('local', new LocalStrategy(...)); // Apply JWT strategy only on protected routes
app.use(passport.initialize()); app.use(passport.session()); // Using multiple heavy strategies without lazy loading passport.use('local', new LocalStrategy(...)); passport.use('jwt', new JwtStrategy(...));
| Pattern | Middleware Overhead | Server Response Delay | User Interaction Impact | Verdict |
|---|---|---|---|---|
| Eagerly load all Passport strategies and session middleware | High (multiple strategies active) | Adds 20-50ms delay | Slower input responsiveness (INP) | [X] Bad |
| Initialize Passport with minimal strategies and no session middleware unless needed | Low (only required strategies) | Adds 5-10ms delay | Better input responsiveness (INP) | [OK] Good |
async validate(username: string, password: string) {
if (username === 'user' && password === 'pass') {
return { userId: 1, username };
}
return null;
}@UseGuards(AuthGuard(LocalStrategy))
@Get('profile')
getProfile() {
return 'User Profile';
}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?Pass the test and this pattern moves unknown