Complete the code to import NextResponse from Next.js.
import { [1] } from 'next/server';
The NextResponse utility is imported from 'next/server' to handle responses like redirects in Next.js middleware.
Complete the code to check if the user is authenticated inside the middleware.
export function middleware(request) {
const token = request.cookies.get('[1]');
if (!token) {
return NextResponse.redirect(new URL('/login', request.url));
}
}The cookie named token is commonly used to store authentication tokens.
Fix the error in the middleware to correctly redirect unauthenticated users.
if (!token) { return [1](new URL('/login', request.url)); }
In Next.js middleware, NextResponse.redirect() is used to redirect the request.
Fill both blanks to allow middleware only on protected routes and exclude public ones.
export const config = {
matcher: ['/dashboard[1]', '/settings[2]']
};The matcher uses wildcards like /* to match all subpaths under a route.
Fill all three blanks to extract the token, check it, and redirect if missing.
export function middleware(request) {
const token = request.cookies.get([1]);
if (!token) {
return NextResponse.[2](new URL([3], request.url));
}
}The cookie name is 'token', the redirect method is redirect on NextResponse, and the URL path is '/login'.