⚡ BolkAuth

bolkAuthMiddleware runs before matched Next.js routes render. It checks the session cookie and redirects anonymous users to the sign-in page.

Basic Setup

import { bolkAuthMiddleware } from "@bolkauth/nextjs";
import { auth } from "@/lib/auth";
 
export default bolkAuthMiddleware(auth, {
  signInUrl: "/sign-in",
  onboardingUrl: "/onboarding",
  publicRoutes: ["/", "/sign-in", "/sign-up", "/api/auth"],
});
 
export const config = {
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

Options

OptionTypeDefaultDescription
signInUrlstring"/sign-in"Redirect destination when no valid session exists.
onboardingUrlstring"/onboarding"Redirect destination for onboarding checks.
publicRoutesstring[][]Routes that bypass auth. Exact routes also match subpaths.
ignoredRoutesstring[][]Reserved option for compatibility.
afterSignInUrlstringNot used by middleware redirectsReserved option for compatibility.

Public Routes

Always include the auth API route and your sign-in/sign-up pages:

publicRoutes: [
  "/",
  "/sign-in",
  "/sign-up",
  "/api/auth",
  "/api/webhooks/stripe",
]

Routes containing *, (.*), or regex-like tokens are treated as patterns.

What Middleware Checks

  1. If the path is public, continue immediately.
  2. Read the session cookie.
  3. Verify the JWT signature with auth.config.secret.
  4. Redirect to signInUrl when the cookie is missing or invalid.
  5. Continue when the session is valid.

Middleware performs a lightweight JWT check and does not fetch the user from the database. Use requireAuth() in pages or actions when you need the user record.

On this page