Consider a Next.js middleware that checks for a valid authentication token in cookies. If the token is missing, the middleware redirects the user to the login page.
What will the user experience when trying to access /dashboard without a token?
import { NextResponse } from 'next/server'; export function middleware(request) { const token = request.cookies.get('authToken'); if (!token) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); } export const config = { matcher: ['/dashboard'] };
Think about what the middleware does when the token is missing.
The middleware checks for the token cookie. If it is missing, it immediately redirects the user to the login page, preventing access to the protected route.
Which option contains a syntax error that will prevent the middleware from running?
import { NextResponse } from 'next/server'; export function middleware(request) { const token = request.cookies.get('authToken') if (!token) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); } export const config = { matcher: ['/profile'] };
Check if JavaScript requires semicolons and if the method calls are correct.
JavaScript does not require semicolons strictly, and the method call 'request.cookies.get('authToken')' is correct. The function declaration syntax is also correct.
The middleware is intended to redirect users without a valid token to the login page. However, unauthenticated users can still access protected pages. What is the likely cause?
import { NextResponse } from 'next/server'; export function middleware(request) { const token = request.cookies.get('authToken'); if (token === undefined) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); } export const config = { matcher: ['/settings'] };
Consider what values request.cookies.get() returns when the cookie is missing.
If the cookie is missing, request.cookies.get() returns null, not undefined. Checking only for undefined misses this case, so unauthenticated users are not redirected.
In Next.js middleware, when redirecting unauthenticated users, what HTTP status code does the response have?
import { NextResponse } from 'next/server'; export function middleware(request) { const token = request.cookies.get('authToken'); if (!token) { return NextResponse.redirect(new URL('/login', request.url)); } return NextResponse.next(); } export const config = { matcher: ['/account'] };
Think about the common status code used for temporary redirects.
NextResponse.redirect uses status code 307 by default, which means a temporary redirect.
Which reason best explains why authentication is often handled in Next.js middleware rather than only on client-side components?
Consider where middleware runs and what it can prevent before the page loads.
Middleware runs on the server edge before the page renders, so it can block unauthorized access early, protecting sensitive data and improving security.