0
0
NestJSframework~10 mins

Session-based authentication 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 session middleware in a NestJS app.

NestJS
import * as session from '[1]';
Drag options to blanks, or click blank then click option'
Abody-parser
Bpassport
Ccookie-parser
Dexpress-session
Attempts:
3 left
💡 Hint
Common Mistakes
Importing the wrong middleware like cookie-parser or body-parser.
Forgetting to import any session middleware.
2fill in blank
medium

Complete the code to apply session middleware in the main app module.

NestJS
app.use(session({ secret: '[1]', resave: false, saveUninitialized: false }));
Drag options to blanks, or click blank then click option'
AmySecret
B12345
Ckeyboard cat
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty string or a weak secret.
Forgetting to set the secret property.
3fill in blank
hard

Fix the error in the session configuration to avoid saving uninitialized sessions.

NestJS
app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: [1] }));
Drag options to blanks, or click blank then click option'
Afalse
Bundefined
Cnull
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Setting saveUninitialized to true causes unnecessary session storage.
Using null or undefined causes configuration errors.
4fill in blank
hard

Fill both blanks to configure cookie settings for secure sessions.

NestJS
app.use(session({ secret: 'keyboard cat', cookie: { secure: [1], maxAge: [2] } }));
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C60000
D3600000
Attempts:
3 left
💡 Hint
Common Mistakes
Setting secure to true without HTTPS causes cookies to not be sent.
Using too short or too long maxAge values.
5fill in blank
hard

Fill all three blanks to create a session-based login route that saves user info.

NestJS
app.post('/login', (req, res) => {
  const { username } = req.body;
  req.session.[1] = [2];
  res.[3]('Logged in');
});
Drag options to blanks, or click blank then click option'
Auser
Busername
Csend
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.session.username instead of req.session.user.
Using res.json() when a simple message is enough.
Forgetting to save user info in the session.