0
0
NextJSframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Client Session Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Next.js client component display?

Consider this Next.js client component that tries to read a session value using useSession from next-auth/react. What will it render if the user is not logged in?

NextJS
import { useSession } from 'next-auth/react';

export default function UserStatus() {
  const { data: session } = useSession();

  if (!session) {
    return <p>Loading session...</p>;
  }

  return <p>Welcome, {session.user.name}!</p>;
}
A<p>Welcome, undefined!</p>
B<p>Error: session is undefined</p>
C<p>Welcome, guest!</p>
D<p>Loading session...</p>
Attempts:
2 left
💡 Hint

Think about what useSession returns before the session is loaded.

state_output
intermediate
2:00remaining
What is the value of userName after this code runs?

This Next.js client component uses useSession and useEffect to set a local state userName. What will userName be after the component mounts if the user is logged in with name "Alice"?

NextJS
import { useSession } from 'next-auth/react';
import { useState, useEffect } from 'react';

export default function Profile() {
  const { data: session } = useSession();
  const [userName, setUserName] = useState('');

  useEffect(() => {
    if (session?.user?.name) {
      setUserName(session.user.name);
    }
  }, [session]);

  return <p>{userName}</p>;
}
A"Alice"
B"" (empty string)
Cundefined
Dnull
Attempts:
2 left
💡 Hint

Remember when useEffect runs and how state updates work.

📝 Syntax
advanced
2:00remaining
Which option correctly accesses session data in a Next.js client component?

Choose the code snippet that correctly uses useSession to access the user's email in a client component.

Aconst { data: session } = useSession(); const email = session.user.email;
Bconst session = useSession(); const email = session.user.email;
Cconst { session } = useSession(); const email = session.user.email;
Dconst { data } = useSession(); const email = data.user.email;
Attempts:
2 left
💡 Hint

Check the exact structure returned by useSession.

🔧 Debug
advanced
2:00remaining
Why does this Next.js client component throw an error?

This component tries to access session data but throws an error. What is the cause?

NextJS
import { useSession } from 'next-auth/react';

export default function Dashboard() {
  const { data: session } = useSession();
  return <p>{session.user.email}</p>;
}
AuseSession cannot be used in client components
Bsession is null initially, so accessing user.email causes a TypeError
CThe component must be server-side to access session
DMissing import for React causes the error
Attempts:
2 left
💡 Hint

Think about the initial value of session when the component first renders.

🧠 Conceptual
expert
3:00remaining
Which statement about client-side session access in Next.js is true?

Choose the correct statement about accessing session data on the client side in Next.js using next-auth.

AClient components can access session data synchronously via props without hooks.
BSession data is always immediately available on the client without delay when using <code>useSession</code>.
CThe <code>useSession</code> hook provides session data asynchronously and may be null initially, so components must handle loading states.
DNext.js requires server components to access session data; client components cannot access it.
Attempts:
2 left
💡 Hint

Consider how useSession works and the nature of client-side data fetching.