fiber

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

README

Fiber Adapter for Authula

A Fiber middleware that bridges Authula's http.Handler to Fiber's fasthttp-based context.

Why?

Fiber uses fasthttp under the hood, not net/http. Authula exposes a standard http.Handler. While Go provides fasthttpadaptor, it can lose the request body — a critical issue for authentication payloads like sign-in and sign-up.

This adapter manually builds net/http requests from Fiber's context, ensuring full request body preservation and correct header propagation (including multi-value Set-Cookie headers).

Installation

go get github.com/Authula/authula

Usage

package main

import (
	"log"

	"github.com/gofiber/fiber/v3"

	authula "github.com/Authula/authula"
	authulaconfig "github.com/Authula/authula/config"
	fiberadapter "github.com/Authula/authula/adapters/fiber"
)

func main() {
	// 1. Create Authula instance
	auth := authula.New(&authula.AuthConfig{
		Config: authulaconfig.NewConfig(
			authulaconfig.WithBaseURL("http://localhost:3000"),
			authulaconfig.WithBasePath("/api/auth"),
			// ... your config
		),
		// ... your plugins
	})

	// 2. Create Fiber app
	app := fiber.New()

	// 3. Mount on /api/auth
	app.Use("/api/auth", fiberadapter.New(fiberadapter.Config{
		Handler: auth.Handler(),
	}))

	// 4. Your app routes
	app.Get("/", func(c fiber.Ctx) error {
		return c.SendString("Hello!")
	})

	log.Fatal(app.Listen(":3000"))
}

Config

Field Type Required Default Description
Handler http.Handler Yes The Authula handler from auth.Handler()
Next func(fiber.Ctx) bool No nil Skip middleware when returning true
ErrorHandler fiber.ErrorHandler No 500 JSON Called on internal adapter errors

How It Works

  1. Reads the raw request body from Fiber's context
  2. Builds a standard *http.Request with all headers, query params, and body
  3. Creates a ResponseWriter that captures the response back into Fiber
  4. Calls handler.ServeHTTP() with the bridged request/response

The key insight is bypassing fasthttpadaptor — it uses fasthttp.Request.BodyStream() which can return an empty reader for certain request types, causing authentication to fail silently.

Documentation

Overview

Package fiber provides a Fiber middleware adapter for authula.

It bridges Fiber's fasthttp-based context to authula's standard net/http.Handler by manually constructing http.Request objects. This avoids fasthttpadaptor which can lose the request body — a critical issue for authentication payloads.

Usage:

auth := authula.New(&authula.AuthConfig{...})

app := fiber.New()
app.Use("/api/auth", fiberadapter.New(fiberadapter.Config{
    Handler: auth.Handler(),
}))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config Config) fiber.Handler

New creates a Fiber middleware that proxies requests to an authula http.Handler. It manually builds net/http requests from Fiber's fasthttp context to ensure request bodies are preserved correctly.

Types

type Config

type Config struct {
	// Handler is the authula http.Handler. Required.
	// Obtain it via auth.Handler().
	Handler http.Handler `json:"-" toml:"-"`

	// Next defines a function to skip this middleware when returning true.
	// Optional. Default: nil (never skip).
	Next func(c fiber.Ctx) bool `json:"-" toml:"-"`

	// ErrorHandler is called when the adapter encounters an internal error
	// (e.g., a malformed request URL). Optional. Default: returns 500 JSON.
	ErrorHandler fiber.ErrorHandler `json:"-" toml:"-"`
}

Config defines the config for the authula Fiber adapter middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL