0
0
NextJSframework~20 mins

Why authentication matters in NextJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Authentication Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is authentication important in web applications?

Which of the following best explains why authentication is crucial in web applications?

AIt ensures only authorized users can access protected resources, preventing unauthorized data access.
BIt speeds up the loading time of web pages by caching user data.
CIt automatically fixes bugs in the application code during runtime.
DIt allows users to browse the website anonymously without any restrictions.
Attempts:
2 left
💡 Hint

Think about what happens if anyone can access private data without verifying who they are.

component_behavior
intermediate
2:00remaining
What happens when a Next.js page requires authentication?

Consider a Next.js page that checks if a user is logged in before showing content. What is the expected behavior if the user is not authenticated?

AThe page loads normally and shows all content regardless of authentication.
BThe page crashes with a server error due to missing user data.
CThe page redirects the user to a login page to authenticate before accessing content.
DThe page shows a blank screen without any message or redirect.
Attempts:
2 left
💡 Hint

Think about how apps protect private pages from users who haven't signed in yet.

state_output
advanced
2:00remaining
What is the output of this Next.js authentication check code?

Given this simplified Next.js component code, what will be rendered if user is null?

NextJS
import { useState, useEffect } from 'react';

export default function Dashboard() {
  const [user, setUser] = useState(null);

  useEffect(() => {
    // Simulate fetching user data
    setTimeout(() => setUser(null), 1000);
  }, []);

  if (!user) {
    return <p>Please log in to view your dashboard.</p>;
  }

  return <p>Welcome back, {user.name}!</p>;
}
A<p>Welcome back, undefined!</p>
B<p>Please log in to view your dashboard.</p>
CNothing is rendered because of an error.
D<p>Loading user data...</p>
Attempts:
2 left
💡 Hint

Check what the component returns when user is null.

📝 Syntax
advanced
2:00remaining
Which Next.js code snippet correctly protects a page with server-side authentication?

Choose the code that correctly uses getServerSideProps to redirect unauthenticated users to the login page.

A
export async function getServerSideProps(context) {
  const user = await getUserFromSession(context.req);
  if (!user) {
    return { props: { redirect: '/login' } };
  }
  return { props: { user } };
}
B
export async function getServerSideProps() {
  const user = await getUserFromSession();
  if (user) {
    return { redirect: { destination: '/login', permanent: false } };
  }
  return { props: { user } };
}
C
export async function getServerSideProps(context) {
  const user = await getUserFromSession(context.req);
  if (!user) {
    redirect('/login');
  }
  return { props: { user } };
}
D
export async function getServerSideProps(context) {
  const user = await getUserFromSession(context.req);
  if (!user) {
    return { redirect: { destination: '/login', permanent: false } };
  }
  return { props: { user } };
}
Attempts:
2 left
💡 Hint

Remember that getServerSideProps must return a redirect object to redirect users.

🔧 Debug
expert
2:00remaining
Why does this Next.js authentication code cause a runtime error?

Examine the code below. Why does it cause a runtime error when the page loads?

NextJS
import { useState, useEffect } from 'react';

export default function Profile() {
  const [user, setUser] = useState();

  useEffect(() => {
    fetch('/api/user')
      .then(res => res.json())
      .then(data => setUser(data.user));
  }, []);

  return <p>Hello, {user.name}!</p>;
}
ABecause <code>user</code> is initially undefined, accessing <code>user.name</code> causes a TypeError.
BBecause the fetch call is missing an await keyword causing a syntax error.
CBecause the API endpoint '/api/user' does not exist, causing a 404 error.
DBecause <code>useEffect</code> is called outside the component function.
Attempts:
2 left
💡 Hint

Think about what happens before the fetch finishes and user is set.