My App

NextAuth Integration with Fumadocs

Learn how to integrate NextAuth middleware with Fumadocs for authentication on documentation sites

Based on my analysis of the Fumadocs codebase, no, Fumadocs does not currently make it simple to wrap documentation sites with NextAuth middleware for authentication.

Current State

  1. No built-in NextAuth integration: There's no mention of NextAuth or authentication middleware anywhere in the codebase.
  2. Own middleware for i18n: Fumadocs provides its own middleware via createI18nMiddleware() (in packages/core/src/i18n/middleware.ts:59-113) which returns a NextProxy function for handling internationalization.
  3. Documentation mentions custom middleware: The i18n docs mention "Using your own middleware?" and suggest you can use your own middleware instead:

    The default middleware is optional, you can also use your own middleware or the one provided by i18n libraries.

  4. No middleware composition utilities: There's no built-in way to chain or compose multiple middleware functions (like combining Fumadocs i18n middleware + NextAuth middleware).

What Users Would Need to Do

To use NextAuth with Fumadocs, users would need to manually compose the middleware functions since Next.js doesn't provide built-in middleware chaining:

// middleware.ts - manual composition required
import { auth } from '@/auth'; // NextAuth
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { i18n } from '@/lib/i18n';

const i18nMiddleware = createI18nMiddleware(i18n);

export default auth((req) => {
  // Authentication logic first
  // Then call i18n middleware
  return i18nMiddleware(req);
});

This requires understanding how both middleware work and manually orchestrating them, which is not straightforward.

The middleware would be implemented in the user's own project, not in the Fumadocs source code.

How It Works

Looking at the architecture:

  1. User's project has the middleware file: In Next.js, middleware is defined in middleware.ts at the root of the user's app, not in node_modules.
  2. Fumadocs only provides a helper: The createI18nMiddleware() function is just a helper that returns a standard Next.js middleware function. Here's the example from the Fumadocs i18n example:
// examples/i18n/proxy.ts (user's project file)
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { i18n } from '@/lib/i18n';

export default createI18nMiddleware(i18n);

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
  1. Users can compose middleware freely: Since the middleware file lives in their project, users can write whatever logic they want:
// User's middleware.ts - they control this file
import { auth } from '@/auth'; // NextAuth
import { createI18nMiddleware } from 'fumadocs-core/i18n/middleware';
import { i18n } from '@/lib/i18n';

const i18nMiddleware = createI18nMiddleware(i18n);

export default async function middleware(request) {
  // Custom auth logic
  const session = await auth();
  if (!session && request.nextUrl.pathname.startsWith('/docs')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }

  // Then handle i18n
  return i18nMiddleware(request);
}

export const config = {
  matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};

The Issue

The problem isn't that users can't do this — they can. The issue is:

  1. Not documented: Fumadocs doesn't provide examples or guidance on how to compose middleware
  2. Not trivial: Users need to understand the return types and execution flow
  3. Easy to get wrong: Middleware composition has edge cases (rewrites, redirects, etc.)

So while users can implement this entirely in their own project without touching Fumadocs source, it would be helpful if Fumadocs:

  • Documented common patterns (NextAuth, other middleware)
  • Provided composition utilities or examples
  • Explained how their middleware interacts with other middleware

The answer is: Users implement it in their own project, but Fumadocs could make it easier with better documentation and examples.

On this page