Built-in Middlewares

JWT and JWKS Authentication

Verify JSON Web Tokens (JWT) and remote JWKS key sets (Auth0, Clerk, Supabase) with built-in typed authentication middleware for Taser.js APIs.

Taser.js includes built-in JWT and JWKS authentication middlewares that verify bearer tokens and inject typed claims directly into ctx.state.


JWT Authentication (@taserjs/router/jwt)

The jwt() middleware extracts the Authorization: Bearer <token> header, verifies the signature using your secret key, and attaches the typed payload to ctx.state.jwtPayload.

src/routes/dashboard.ts
import { jwt } from "@taserjs/router/jwt";
import { t } from "@taserjs/router";

type JwtClaims = {
  sub: string;
  email: string;
  role: "user" | "admin";
};

export default t.layout("/dashboard").use(
  jwt<JwtClaims>({
    secret: process.env.JWT_SECRET!,
    alg: "HS256",
  }),
);

Accessing Claims in Downstream Routes

Any route inside src/routes/dashboard/ receives ctx.state.jwtPayload with complete type inference:

src/routes/dashboard/profile.get.ts
import { json } from "@taserjs/router/reply";
import { t } from "@taserjs/router";

export default t.get("/dashboard/profile").handler(async (ctx) => {
  // 100% typed from JwtClaims:
  const userId = ctx.state.jwtPayload.sub;
  const userEmail = ctx.state.jwtPayload.email;
  const userRole = ctx.state.jwtPayload.role;

  const user = await ctx.db.getUser(userId);
  return json(user);
});

JWKS Authentication (@taserjs/router/jwk)

For auth providers like Auth0, Clerk, Firebase, AWS Cognito, or Supabase, use @taserjs/router/jwk to verify tokens against remote JSON Web Key Sets:

src/routes/api.ts
import { jwk } from "@taserjs/router/jwk";
import { t } from "@taserjs/router";

type ClerkPayload = {
  sub: string;
  iss: string;
  azp?: string;
};

export default t.layout("/api").use(
  jwk<ClerkPayload>({
    jwks_uri: "https://clerk.your-domain.com/.well-known/jwks.json",
  }),
);

Configuration Options

JWT Options

Prop

Type

JWK Options

Prop

Type


Next Steps