Consider a NestJS app using session-based authentication. When a user logs in successfully, what is stored in the session?
Think about what data should stay private and where it is safest to store it.
In session-based authentication, the server stores user data linked to a session ID. The client only holds the session ID in a cookie. This keeps sensitive data secure on the server.
Choose the correct way to configure session middleware in a NestJS app using express-session.
Look for the option that includes all required properties with recommended values.
Option A correctly sets secret, disables resave, and disables saveUninitialized, which is a common secure setup.
Given this NestJS controller snippet, the session does not persist after login. What is the likely cause?
async login(@Req() req) {
req.session.user = { id: 1, name: 'Alice' };
return 'Logged in';
}Think about middleware order and when sessions become available.
If session middleware is not set up before routes, req.session will be undefined or not saved, so session data won't persist.
In this logout method, what will be the value of req.session.user after calling req.session.destroy()?
async logout(@Req() req) {
req.session.destroy(err => {
if (err) throw err;
});
return 'Logged out';
}Consider what happens to session data after destroy is called.
Calling req.session.destroy removes the session data, so req.session.user becomes undefined.
Choose the best explanation why session-based authentication is stateful while token-based is stateless.
Think about where user login info is stored and how the server tracks sessions.
Session-based auth keeps user info on the server linked to a session ID, so the server must remember state. Token-based auth encodes user info in the token itself, so the server does not store session state.