Basic Auth Middleware #

This middleware can apply Basic authentication to a specified path. Implementing Basic authentication with Cloudflare Workers or others is more complicated than it seems, but with this, it’s a snap.

Import #

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

Usage #

const app = new Hono()

app.use(
  '/auth/*',
  basicAuth({
    username: 'hono',
    password: 'acoolproject',
  })
)

app.get('/auth/page', (c) => {
  return c.text('You are authorized')
})

Tips #

For Fastly [email protected], polyfill crypto or use crypto-js.

Install:

npm i crypto-js

Override hashFunction:

import { SHA256 } from 'crypto-js'

app.use(
  '/auth/*',
  basicAuth({
    username: 'hono',
    password: 'acoolproject',
    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 :)