Skip to content

Route Middleware

Route Middleware#

Use _middleware.ts files to colocate Hono-compatible middleware with routes.

import { defineMiddleware, redirect } from "@kamod-ch/otok/server";

export default defineMiddleware(async (c, next) => {
  const user = c.get("user");
  if (!user) redirect("/login", 303);
  await next();
});

Middleware runs from parent to child and applies to loaders, actions, and rendering.

src/app/routes/_middleware.ts
src/app/routes/admin/_middleware.ts
src/app/routes/admin/index.tsx

Global API routes registered through createOtokApp({ configure }) remain normal Hono routes.