honojs-middleware/packages/cloudflare-access
Jonathan Haines 9235709060
refactor: composite build (#1230)
* refactor: composite build

* chore(ua-blocker): move demo.ts out of src
2025-06-16 11:23:47 +09:00
..
src refactor(cloudflare-access): enable isolated declarations (#1195) 2025-06-09 18:12:04 +09:00
CHANGELOG.md Version Packages (#1206) 2025-06-09 19:05:17 +09:00
README.md chore: add coverage badges (#1023) 2025-03-19 17:53:11 +09:00
package.json Version Packages (#1206) 2025-06-09 19:05:17 +09:00
tsconfig.build.json refactor: composite build (#1230) 2025-06-16 11:23:47 +09:00
tsconfig.json refactor: composite build (#1230) 2025-06-16 11:23:47 +09:00
tsconfig.spec.json feat(eslint-config): enable linting with type information (#1098) 2025-04-07 19:31:09 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

README.md

Cloudflare Access middleware for Hono

codecov

This is a Cloudflare Access third-party middleware for Hono.

This middleware can be used to validate that your application is being served behind Cloudflare Access by verifying the JWT received, User details from the JWT are also available inside the request context.

This middleware will also ensure the Access policy serving the application is from a specific Access Team.

Usage

import { cloudflareAccess } from '@hono/cloudflare-access'
import { Hono } from 'hono'

const app = new Hono()

app.use('*', cloudflareAccess('my-access-team-name'))
app.get('/', (c) => c.text('foo'))

export default app

Access JWT payload

import { cloudflareAccess, CloudflareAccessVariables } from '@hono/cloudflare-access'
import { Hono } from 'hono'

type myVariables = {
  user: number
}

const app = new Hono<{ Variables: myVariables & CloudflareAccessVariables }>()

app.use('*', cloudflareAccess('my-access-team-name'))
app.get('/', (c) => {
  const payload = c.get('accessPayload')

  return c.text(`You just authenticated with the email ${payload.email}`)
})

export default app

Errors throw by the middleware

Error HTTP Code
Authentication error: Missing bearer token 401
Authentication error: Unable to decode Bearer token 401
Authentication error: Token is expired 401
Authentication error: Expected team name {your-team-name}, but received ${different-team-signed-token} 401
Authentication error: Invalid Token 401
Authentication error: The Access Organization 'my-team-name' does not exist 500
Authentication error: Received unexpected HTTP code 500 from Cloudflare Access 500

Author

Gabriel Massadas https://github.com/g4brym

License

MIT