Repository: honojs/hono
File: src/middleware/basic-auth/index.ts (TypeScript)

Code before:
      realm?: string
      hashFunction?: Function
      invalidUserMessage?: string | object | MessageFunction
      /**
       * If set, the authenticated username will be stored in the context.
       * - `true`: stores under the key `'basicAuthUsername'`
       * - `string`: stores under the specified custom key
       */
      usernameContextKey?: boolean | string
      /**
       * Callback function called when authentication succeeds.
       * Useful for custom logic like logging or setting additional context variables.
       */
      onAuthSuccess?: (c: Context, username: string) => void | Promise<void>
    }
  | {
      verifyUser: (username: string, password: string, c: Context) => boolean | Promise<boolean>
      realm?: string
      hashFunction?: Function
      invalidUserMessage?: string | object | MessageFunction
      /**
       * If set, the authenticated username will be stored in the context.
       * - `true`: stores under the key `'basicAuthUsername'`
       * - `string`: stores under the specified custom key
       */
      usernameContextKey?: boolean | string
      /**
       * Callback function called when authentication succeeds.
       * Useful for custom logic like logging or setting additional context variables.
       */
      onAuthSuccess?: (c: Context, username: string) => void | Promise<void>
    }

/**
 * Basic Auth Middleware for Hono.
 *
 * @see {@link https://hono.dev/docs/middleware/builtin/basic-auth}
 *
 * @param {BasicAuthOptions} options - The options for the basic authentication middleware.
 * @param {string} options.username - The username for authentication.
 * @param {string} options.password - The password for authentication.
 * @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
 * @param {Function} [options.hashFunction] - The hash function used for secure comparison.
 * @param {Function} [options.verifyUser] - The function to verify user credentials.
 * @param {string | object | MessageFunction} [options.invalidUserMessage="Unauthorized"] - The invalid user message.
 * @param {boolean | string} [options.usernameContextKey] - If set, stores the authenticated username in context.
 * @param {Function} [options.onAuthSuccess] - Callback function called on successful authentication.
 * @returns {MiddlewareHandler} The middleware handler function.
 * @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
 *
 * @example

Review comment (security):
`usernameContextKey` is needed? I think it is unnecessary. You can use the following method:

```ts
basicAuth({
  username: 'hono',
  password: 'secret',
  onAuthSuccess: (c, username) => {
    c.set('basicAuthUsername', username)
  },
})
```

It's simple.

Code after:
      realm?: string
      hashFunction?: Function
      invalidUserMessage?: string | object | MessageFunction
      onAuthSuccess?: (c: Context, username: string) => void | Promise<void>
    }
  | {
      verifyUser: (username: string, password: string, c: Context) => boolean | Promise<boolean>
      realm?: string
      hashFunction?: Function
      invalidUserMessage?: string | object | MessageFunction
      onAuthSuccess?: (c: Context, username: string) => void | Promise<void>
    }

/**
 * Basic Auth Middleware for Hono.
 *
 * @see {@link https://hono.dev/docs/middleware/builtin/basic-auth}
 *
 * @param {BasicAuthOptions} options - The options for the basic authentication middleware.
 * @param {string} options.username - The username for authentication.
 * @param {string} options.password - The password for authentication.
 * @param {string} [options.realm="Secure Area"] - The realm attribute for the WWW-Authenticate header.
 * @param {Function} [options.hashFunction] - The hash function used for secure comparison.
 * @param {Function} [options.verifyUser] - The function to verify user credentials.
 * @param {string | object | MessageFunction} [options.invalidUserMessage="Unauthorized"] - The invalid user message.
 * @param {Function} [options.onAuthSuccess] - Callback function called on successful authentication.
 * @returns {MiddlewareHandler} The middleware handler function.
 * @throws {HTTPException} If neither "username and password" nor "verifyUser" options are provided.
 *
 * @example
 * ```ts
 * const app = new Hono()
 *
 * app.use(
 *   '/auth/*',
 *   basicAuth({
 *     username: 'hono',
 *     password: 'ahotproject',
 *   })
 * )
 *
 * app.get('/auth/page', (c) => {
 *   return c.text('You are authorized')
 * })
 * ```
 *
 * @example
 * ```ts
 * // With onAuthSuccess callback
 * app.use(
 *   '/auth/*',