middleware-protection

Protect Next.js page and API routes with centralized authentication middleware.

Updated Jan 30, 2026
One-click install
npx skills add https://github.com/jrmatherly/metorial-workspace --skill middleware-protection
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: middleware-protection
Source: https://github.com/jrmatherly/metorial-workspace/tree/main/.github/skills/middleware-protection
Command: npx skills add https://github.com/jrmatherly/metorial-workspace --skill middleware-protection

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Protects Next.js routes with a centralized authentication middleware that checks authentication once and supports role-based access control for both pages and API routes.

Core Features & Use Cases

  • Declarative route protection for pages and API routes.
  • Role-based access control, session refresh, and consistent error handling.
  • Use case: protect dashboards, settings, and admin endpoints while exposing public pages.

Quick Start

Copy the middleware.ts into your Next.js project, customize the PROTECTED_ROUTES and PUBLIC_ROUTES arrays, ensure environment variables for your auth provider, and test protected vs public routes.

Frequently Asked Questions about middleware-protection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I protect Next.js routes with centralized authentication middleware?

Centralized authentication middleware protects Next.js routes by checking authentication once and supporting role-based access control for both pages and API routes. It uses declarative route configuration to define public, protected, and role-based access levels.

Can I apply role-based access control to both page and API routes in Next.js?

Yes, role-based access control can be applied to both page and API routes in Next.js. The middleware allows you to declaratively configure route protection, ensuring consistent error handling and user context propagation via response headers across your application.

What is the best way to configure public and protected routes in a Next.js app?

The best way to configure public and protected routes is by customizing the PUBLIC_ROUTES and PROTECTED_ROUTES arrays in your middleware file. This declarative route protection method allows you to expose public pages while securing dashboards and admin endpoints.

Does centralized middleware support session management and session refreshing in Next.js?

Yes, centralized middleware supports session management and session refreshing in Next.js. It handles session refresh automatically while propagating user context via response headers, ensuring authenticated sessions remain active during route transitions.

How do I set up environment variables for authentication middleware in Next.js?

To set up environment variables for authentication middleware, configure your auth provider credentials in the Next.js environment. You then copy the middleware file into your project and test protected versus public routes to ensure proper authentication checks.

Why should I use centralized route security instead of per-page authentication checks?

Centralized route security checks authentication once and applies it across the entire Next.js application, preventing repetitive per-page checks. It ensures consistent error handling, declarative route configuration, and unified role-based access control for both pages and APIs.