Basic Auth
Basic auth middleware provides an HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized
for missing or invalid credentials.
Installation
- go get -u github.com/gofiber/basicauth
Signature
- basicauth.New(config ...Config) func(*fiber.Ctx)
Config
Property | Type | Description | Default |
---|---|---|---|
Filter | func(fiber.Ctx) bool | Defines a function to skip middleware | nil |
Users | map[string][string] | Users defines the allowed credentials | nil |
Realm | string | Realm is a string to define the realm attribute | Restricted |
Authorizer | func(string, string) bool | A function you can pass to check the credentials however you want. | nil |
Unauthorized | func(fiber.Ctx) | Custom response body for unauthorized responses | nil |
Example
- package main
- import (
- "github.com/gofiber/fiber"
- "github.com/gofiber/basicauth"
- )
- func main() {
- app := fiber.New()
- cfg := basicauth.Config{
- Users: map[string]string{
- "john": "doe",
- "admin": "123456",
- },
- }
- app.Use(basicauth.New(cfg))
- app.Get("/", func(c *fiber.Ctx) {
- c.Send("Welcome!")
- })
- app.Listen(3000)
- }