Bird
0
0

Identify the error in this NestJS session setup code:

medium📝 Debug Q14 of 15
NestJS - Authentication
Identify the error in this NestJS session setup code:
import { Module } from '@nestjs/common';
import { SessionModule } from '@nestjs/session';

@Module({
  imports: [
    SessionModule.forRoot({
      secret: 'mySecret',
      resave: false,
      saveUninitialized: true,
    }),
  ],
})
export class AppModule {}
AMissing 'cookie' configuration inside forRoot options
BThe secret should be an environment variable, not a string literal
C'resave' and 'saveUninitialized' are not valid options for SessionModule
DSessionModule.forRoot should be replaced with SessionModule.register
Step-by-Step Solution
Solution:
  1. Step 1: Review SessionModule options

    In '@nestjs/session', options like 'resave' and 'saveUninitialized' are not valid; they belong to express-session directly.
  2. Step 2: Identify correct option usage

    SessionModule expects options like 'secret' and 'cookie', but not 'resave' or 'saveUninitialized'.
  3. Final Answer:

    'resave' and 'saveUninitialized' are not valid options for SessionModule -> Option C
  4. Quick Check:

    Invalid options in SessionModule config = B [OK]
Quick Trick: Check if options belong to NestJS SessionModule or express-session [OK]
Common Mistakes:
  • Adding express-session options directly to SessionModule
  • Confusing 'forRoot' with 'register'
  • Ignoring required cookie config

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes