Complete the code to import the Next.js authentication hook.
import { [1] } from 'next-auth/react';
The useSession hook is used to access authentication status in Next.js apps.
Complete the code to check if a user is authenticated.
const { data: session } = [1]();
if (session) {
// user is authenticated
}useSession() returns session data and status for authentication.
Fix the error in the code to redirect unauthenticated users.
if (!session) { [1]('/api/auth/signin'); }
In Next.js server components, redirect() is used to send users to login.
Fill both blanks to create a protected page that redirects unauthenticated users.
"use client"; import { [1] } from 'next-auth/react'; import { [2] } from 'next/navigation'; export default function Page() { const { data: session } = [1](); if (!session) [2]('/api/auth/signin'); return <main>Welcome, authenticated user!</main>; }
useSession gets the session data, and redirect sends unauthenticated users to sign in.
Fill all three blanks to implement a client component that shows login status and a sign out button.
"use client"; import { [1], [2], [3] } from 'next-auth/react'; export default function AuthStatus() { const { data: session } = [1](); if (!session) return <button onClick={() => [3]()}>Sign In</button>; return ( <> <p>Signed in as {session.user.email}</p> <button onClick={() => [2]()}>Sign Out</button> </> ); }
signIn and signOut functions.getSession in client component.useSession accesses session data, signOut logs out, and signIn starts login.
