Bird
0
0

Consider this NestJS controller method using sessions:

medium📝 component behavior Q4 of 15
NestJS - Authentication
Consider this NestJS controller method using sessions:

@Get('dashboard')
getDashboard(@Session() session: Record) {
  if (session.isAuthenticated) {
    return `Welcome back, ${session.username}`;
  }
  return 'Access denied';
}

What will be the response if a user with session.isAuthenticated = true and session.username = 'Alice' accesses /dashboard?
A"Welcome back, Alice"
B"Access denied"
C"Welcome back, undefined"
DAn error is thrown due to missing session
Step-by-Step Solution
Solution:
  1. Step 1: Check session properties

    The session has isAuthenticated = true and username = 'Alice'.
  2. Step 2: Evaluate the if condition

    Since session.isAuthenticated is true, the method returns the welcome message with the username.
  3. Final Answer:

    "Welcome back, Alice" -> Option A
  4. Quick Check:

    Authenticated session returns personalized greeting [OK]
Quick Trick: Authenticated session returns personalized message [OK]
Common Mistakes:
  • Assuming session properties are undefined
  • Expecting 'Access denied' despite valid session
  • Thinking an error occurs without explicit error handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes