Guest User

Untitled

a guest
Feb 2nd, 2026
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { NextResponse } from "next/server";
  2. import { isAdminFunc } from "./lib/auth";
  3.  
  4. export const matcher = ['/(?!api|_next/static|_next/image|favicon.ico).*)'];
  5.  
  6. export function middleware(request) {
  7.   const url = request.nextUrl.clone();
  8.  
  9.   console.log('Middleware triggered for URL:', url.pathname);
  10.  
  11.   if (url.pathname.startsWith('/admin')) {
  12.     const isAdmin = isAdminFunc(request);
  13.  
  14.     if (!isAdmin) {
  15.       return new NextResponse(`<html><body><h1>Unauthorized</h1><p>You're not admin. Please login</p><!--Request Forbidden by Next.js 15.1.1 Middleware--></body></html>`, {
  16.        status: 401,
  17.        headers: {
  18.          'Content-Type': 'text/html'
  19.        }
  20.      });
  21.    }
  22.  }
  23.  
  24.  if (url.pathname.startsWith('/api')) {
  25.    return NextResponse.next({
  26.      headers: request.headers //required for api routes for decoding Auth Header
  27.    });
  28.  }
  29.  
  30.  return NextResponse.next();
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment