← Back to Home

How to Implement JWT Authentication in Hono on Cloudflare Workers

Updated March 5, 2026
jwthonocloudflare workersauth middlewarebearer token

JWT Authentication in Hono

Use stateless JWT auth with route-level middleware.

Token Service

const payload = { username, iat: now, exp: now + 7 * 24 * 60 * 60 };
const token = await sign(payload, env.JWT_SECRET);

Middleware Pattern

Apply auth to /api/*, but skip only public auth endpoints.

const publicAuthRoutes = ['/api/auth/login', '/api/auth/logout'];
if (!authHeader || !authHeader.startsWith('Bearer ')) {
  return c.json({ error: 'Unauthorized', message: 'Missing authentication token' }, 401);
}

Endpoint Contract

Security Notes