Files
SnowLuma/.pnpm-store/v11/files/7a/28df974480d5a62f8cf52b92e3a7f8b329d077be7c722cf3dfdf364fb5e956d80950c928b2b7892a6065b1385197302d13724e4ba7a92c197ee6f3cf90b931
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

74 lines
2.5 KiB
Plaintext

import { MiddlewareHandler } from "../../types.js";
import { Context } from "../../context.js";
//#region src/middleware/basic-auth/index.d.ts
type MessageFunction = (c: Context) => string | object | Promise<string | object>;
type BasicAuthOptions = {
username: string;
password: string;
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/*',
* basicAuth({
* username: 'hono',
* password: 'ahotproject',
* onAuthSuccess: (c, username) => {
* c.set('user', { name: username, role: 'admin' })
* console.log(`User ${username} authenticated`)
* },
* })
* )
* ```
*/
export declare const basicAuth: (options: BasicAuthOptions, ...users: {
username: string;
password: string;
}[]) => MiddlewareHandler;
//#endregion
export {};