Complete the code to import the hook used for client-side session access in Next.js.
import { [1] } from 'next-auth/react';
The useSession hook is used to access the session data on the client side in Next.js.
Complete the code to get the session data and loading state from the hook.
const { data: session, [1] } = useSession();The useSession hook returns an object with data and status. The status indicates if the session is loading, authenticated, or unauthenticated.
Fix the error in the code to correctly check if the user is authenticated.
if (session && session.user[1]) { console.log('User is logged in'); }
Using != null checks for both null and undefined, which is a common way to verify if session.user exists.
Fill both blanks to correctly display the user's email if authenticated.
return (<div>{session?.user[1] ? session.user[2] : 'Not logged in'}</div>);
The optional chaining operator ?. safely accesses email without errors if user is undefined. The first blank checks if the email exists, the second accesses it.
Fill all three blanks to fetch session data client-side and handle loading and unauthenticated states.
const { [1]: session, [2] } = useSession();
if ([3] === 'loading') return <p>Loading...</p>;
if (!session) return <p>Please sign in</p>;The useSession hook returns data as session and status for loading state. We check if status is 'loading' to show a loading message.