Bearer Auth Middleware #

Bearer Auth Middleware provides authentication by verifying API tokens in Request header. The HTTP clients accessing the endpoint will add Authorization header with the Bearer {token} as the value.

With curl command, it would look like this.

curl -H 'Authorization: Bearer honoiscool' http://localhost:8787/auth/page

Import #

import { Hono } from 'hono'
import { bearerAuth } from 'hono/bearer-auth'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { bearerAuth } from 'https://deno.land/x/hono/middleware.ts'

Usage #

const app = new Hono()

const token = 'honoiscool'

app.use('/api/*', bearerAuth({ token }))

app.get('/api/page', (c) => {
  return c.json({ message: 'You are authorized' })
})

Options #

app.use(
  '/auth/*',
  bearerAuth({
    token: 'honoiscool', // Required
    realm: 'example.com',
    prefix: 'Bot'
    hashFunction: (d: string) => SHA256(d).toString(), // For Fastly [email protected]
  })
)
https://github.com/honojs/hono
© Hono developers.
Built with Hugo & Hugo Book Theme - Not Hono :)