Basic Auth
@gaman/basic-auth is the official middleware for adding Basic Authentication in the GamanJS framework.
Basic Auth is a simple mechanism to protect endpoints using a combination of username and password sent via the Authorization header.
Install
Section titled “Install”GamanJS is a framework for backend applications, you can install using your favorite package manager:
npm install @gaman/basic-authpnpm install @gaman/basic-authyarn install @gaman/basic-authbun install @gaman/basic-authHow to Use
Section titled “How to Use”here is how to implement @gaman/basic-auth
import { basicAuth } from "@gaman/basic-auth"
defineBootstrap((app) => { app.mount( basicAuth({ username: 'admin', password: 'admin123' }) ) ...})if you want specific to a certain route just implement like this
import { basicAuth } from "@gaman/basic-auth"
route.get('/user').middleware(basicAuth({ username: 'admin', password: 'admin123'}))Config
Section titled “Config”Here Basic Auth has 2 different configs one static one dynamic.
Static Config
Section titled “Static Config”here is an example of static config
basicAuth({ username: 'admin', password: "admin123",})This configuration is very simple and suitable for basic endpoint protection.
Dynamic Config
Section titled “Dynamic Config”here is an example of dynamic config
basicAuth({ verifyAuth: (username, password, ctx) => { return username === process.env.USERNAME && password === process.env.PASSWORD }})This configuration is suitable if you want:
-
Take credentials from environment variable.
-
Perform validation against database.
-
Create more flexible authentication logic.
Default Config
Section titled “Default Config”here there is additional default config namely
| Name | Type | Default | Description |
|----------------------|-----------------------------------|----------------|-----------|
| realm | string | "Secure Area" | Protection area label displayed on browser login prompt. |
| invalidAuthMessage | string \| object \| function | undefined | Message sent when authentication fails. Can be string, object (JSON), or function async (ctx) => string \| object. |
And there are 3 more default middleware configs please check the following page Middleware Config.