Skip to main content
Alvin QuachFull Stack Developer
HomeProjectsExperienceBlog
HomeProjectsExperienceBlog
alvinquach

Full Stack Developer building systems that respect complexity.

Open to opportunities

AQ

Projects

  • All Projects
  • Hoparc Physical Therapy
  • OpportunIQ
  • Hoop Almanac
  • SculptQL

Knowledge

  • Blog
  • Experience
  • Interview Prep

Connect

  • Contact
  • LinkedIn
  • GitHub
  • X

Resources

  • Resume
© 2026All rights reserved.
Back to Blogs
Tutorial
Depth: ●●○○○

Next.js Middleware: Authentication, Redirects, and Edge Logic

How to use Next.js Middleware for authentication guards, role-based redirects, geolocation, and request modification. Runs at the edge before your pages load.

Published July 30, 20251 min readImportance: ★★★★☆
Share:

Summary

This is a strong, production-ready Next.js middleware pattern that:

Uses Supabase SSR client correctly at the Edge

Skips static assets and non-critical paths for performance

Implements clear route tiers: public, protected, admin

Adds timeouts and graceful failure handling

Preserves redirect intent after login

Supports advanced user validation (banned/inactive) and preview mode

Below is a distilled, reusable version that combines the best ideas from your OpportunIQ and Hoop Almanac setups into a single, opinionated middleware you can drop into other projects.

How to Adapt This Pattern

Change route tiers: Edit PUBLIC_PATHS, PROTECTED_PATHS, and ADMIN_PATHS.

Swap auth provider: Replace the Supabase client with your auth library but keep the same control flow.

Customize validation: Extend validateUser with your own flags (e.g. emailVerified, plan, trialExpired).

Preview/demo flows: Adjust the isPreviewMode logic and exceptions around onboarding or demo routes.

This gives you a single, centralized, edge-optimized guardrail for authentication, authorization, and user state handling across your app.