tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
43 lines • 1.77 kB
text/typescript
/**
* Creates an Express middleware for Basic HTTP Authentication.
*
* This middleware checks for an `Authorization` header and validates
* the username and password using base64-decoded credentials.
*
* If the credentials match, `next()` is called to proceed to the next middleware.
* If the credentials are invalid or missing, a 401 Unauthorized response is sent.
*
* @function auth
* @param {Object} data - Configuration object.
* @param {string} data.login - The expected username.
* @param {string} data.password - The expected password.
* @param {function(import('express').Request, import('express').Response): import('express').Response} [data.customError] - Optional function `(req, res) => {}` called on auth failure.
* @param {Function} [callback] - Optional fallback function `(req, res, next) => {}` executed after customError.
*
* @returns {function(import('express').Request, import('express').Response, import('express').NextFunction): void} Express middleware function.
* @deprecated
*
* @example
* import express from 'express';
* import auth from './auth.mjs';
*
* const app = express();
*
* app.use(auth({
* login: 'admin',
* password: '1234',
* customError: (req, res) => {
* res.send('Custom unauthorized message');
* }
* }));
*
* app.get('/', (req, res) => {
* res.send('Hello, authenticated user!');
* });
*/
export default function auth(data: {
login: string;
password: string;
customError?: ((arg0: import("express").Request, arg1: import("express").Response) => import("express").Response) | undefined;
}, callback?: Function): (arg0: import("express").Request, arg1: import("express").Response, arg2: import("express").NextFunction) => void;
//# sourceMappingURL=auth.d.mts.map