Complete the code to import the Next.js router hook.
import { [1] } from 'next/navigation';
useRoute or useNavigate which are from other frameworks.The useRouter hook is the correct way to access routing in Next.js App Router.
Complete the code to redirect unauthenticated users to the login page.
if (!user) { router.[1]('/login'); }
redirect which is a server function, not a router method.The push method on the router navigates to a new route programmatically.
Fix the error in the conditional route check to correctly protect the page.
export default function Page() {
const router = useRouter();
const user = getUser();
if ([1]) {
router.push('/login');
}
return <main>Welcome!</main>;
}user !== undefined which is true when user exists.Checking !user correctly detects if the user is not logged in.
Fill both blanks to create a conditional rendering that shows login or dashboard links.
return ( <nav> {user ? <a href=[1]>Dashboard</a> : <a href=[2]>Login</a>} </nav> );
When the user exists, link to /dashboard. Otherwise, link to /login.
Fill all three blanks to create a server component that redirects unauthenticated users and shows content otherwise.
import { [1] } from 'next/navigation'; export default function ProtectedPage() { const user = getUser(); if (!user) { [2]('/login'); } return <main>Welcome, [3]!</main>; }
router.push in server components, which is client-only.Use the server-side redirect function to send unauthenticated users to login. Show the user's name in the welcome message.