0
0
NextJSframework~20 mins

Server-side session access in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Server-side Session Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js server component accessing session?

Consider this Next.js server component that reads a user session and renders a greeting. What will it display if the session contains { user: { name: 'Alice' } }?

NextJS
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/lib/authOptions';

export default async function Greeting() {
  const session = await getServerSession(authOptions);
  return (
    <main>
      <h1>Hello, {session?.user?.name ?? 'Guest'}!</h1>
    </main>
  );
}
A<h1>Hello, Alice!</h1>
B<h1>Hello, Guest!</h1>
CError: getServerSession is not a function
D<h1>Hello, undefined!</h1>
Attempts:
2 left
💡 Hint

Check how the session object is accessed and the fallback value used.

📝 Syntax
intermediate
2:00remaining
Which option correctly imports and uses getServerSession in Next.js 14+?

Next.js 14+ requires correct import paths for server session access. Which option shows the correct import and usage of getServerSession?

A
import { getServerSession } from 'next-auth/client';
const session = await getServerSession(authOptions);
B
import { getServerSession } from 'next-auth/server';
const session = await getServerSession(authOptions);
C
import { getServerSession } from 'next-auth/next';
const session = await getServerSession(authOptions);
D
import getServerSession from 'next-auth';
const session = getServerSession(authOptions);
Attempts:
2 left
💡 Hint

Check the official Next.js 14+ documentation for the correct import path.

state_output
advanced
2:00remaining
What is the value of userEmail after this server action runs?

Given this Next.js server action that reads the session and extracts the email, what is the value of userEmail if the session is { user: { email: 'bob@example.com' } }?

NextJS
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/lib/authOptions';

export async function getUserEmail() {
  const session = await getServerSession(authOptions);
  const userEmail = session?.user?.email ?? 'no-email';
  return userEmail;
}
A'no-email'
B'bob@example.com'
Cundefined
Dnull
Attempts:
2 left
💡 Hint

Look at the fallback value used when email is missing.

🔧 Debug
advanced
2:00remaining
What error does this Next.js server component raise?

This server component tries to access session but has a bug. What error will it raise?

NextJS
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/lib/authOptions';

export default function Profile() {
  const session = getServerSession(authOptions);
  return <p>{session?.user?.name}</p>;
}
AError: authOptions is undefined
BNo error, renders user name
CError: JSX must be wrapped in a fragment
DError: 'await' is required for getServerSession
Attempts:
2 left
💡 Hint

Check if the async function is awaited properly.

🧠 Conceptual
expert
3:00remaining
Which statement about server-side session access in Next.js 14+ is TRUE?

Choose the correct statement about accessing sessions on the server in Next.js 14+ with next-auth.

AgetServerSession must be called inside a server component or server action and returns a Promise resolving to the session object or null.
BgetServerSession requires passing the HTTP request and response objects explicitly in Next.js 14+.
CSession data is automatically injected into all components without explicit calls to getServerSession.
DgetServerSession can be called directly in client components without async/await and returns session synchronously.
Attempts:
2 left
💡 Hint

Recall how server components and async data fetching work in Next.js 14+.